Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play an AudioBufferSourceNode twice?

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)
like image 353
alltom Avatar asked Feb 24 '12 23:02

alltom


2 Answers

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).

like image 113
Boris Smus Avatar answered Oct 14 '22 13:10

Boris Smus


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.com/tag/webaudio
like image 45
molokoloco Avatar answered Oct 14 '22 13:10

molokoloco