最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Play an AudioBufferSourceNode twice? - Stack Overflow

programmeradmin5浏览0评论

Should I be able to use the same AudioBufferSourceNode to play a sound multiple times? For some reason, calling noteGrainOn a second time doesn't play audio, even with an intervening noteOff.

This code only plays the sound once:

var node = audioContext.createBufferSource()
node.buffer = audioBuffer
node.connect(audioContext.destination)

var now = audioContext.currentTime
node.noteGrainOn(now, 0, 2)
node.noteOff(now + 2)
node.noteGrainOn(now + 3, 0, 2)
node.noteOff(now + 5)

Should I be able to use the same AudioBufferSourceNode to play a sound multiple times? For some reason, calling noteGrainOn a second time doesn't play audio, even with an intervening noteOff.

This code only plays the sound once:

var node = audioContext.createBufferSource()
node.buffer = audioBuffer
node.connect(audioContext.destination)

var now = audioContext.currentTime
node.noteGrainOn(now, 0, 2)
node.noteOff(now + 2)
node.noteGrainOn(now + 3, 0, 2)
node.noteOff(now + 5)
Share Improve this question asked Feb 24, 2012 at 23:35 alltomalltom 3,2524 gold badges32 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 25

Once you've played back a source node, you can't reuse it. You need to create another AudioBufferSourceNode with the same buffer. Check out the Web Audio FAQ for more info (see the noteOn() question).

I have some hack to toggle play/pause easily :) You can map the audio to an existing audio tag directly and use the control on it

<audio id="sound" src="./test.mp3" controls="controls"></audio>

var audioElement = document.getElementById('sound');
source = audioContext.createMediaElementSource(audioElement);
/* ... */
audioElement.pause();

My example running here : http://www.b2bweb.fr/bonus/Braincer/ I currently use Chrome build 18.0.xxxx

Others refs :

  • Working example for MediaElementAudioSourceNode with Chrome Canary?
  • http://updates.html5rocks./tag/webaudio
发布评论

评论列表(0)

  1. 暂无评论