Is it possible to add event listeners to web audio api sounds? I've been looking for an event or trigger for when a sounds completes but can't find anything. Here is how I imagine it would work:
soundSource = context.createBufferSource();
soundBuffer = context.createBuffer(audioData, true);
soundSource.buffer = soundBuffer;
soundSource.connect(volumeNode);
soundSource.addEventListener('ended', function(e){
console.log("ended", "", e);
}, false);
soundSource.noteOn(context.currentTime);
Is it possible to add event listeners to web audio api sounds? I've been looking for an event or trigger for when a sounds completes but can't find anything. Here is how I imagine it would work:
soundSource = context.createBufferSource();
soundBuffer = context.createBuffer(audioData, true);
soundSource.buffer = soundBuffer;
soundSource.connect(volumeNode);
soundSource.addEventListener('ended', function(e){
console.log("ended", "", e);
}, false);
soundSource.noteOn(context.currentTime);
Share
Improve this question
asked Nov 29, 2012 at 0:51
fatlinesofcodefatlinesofcode
1,66718 silver badges23 bronze badges
3 Answers
Reset to default 12var isFinished = false;
var source = context.createBufferSource();
source.onended = onEnded;
function onEnded() {
isFinished = true;
console.log('playback finished');
}
Check this out
Not today, no. I know there's been discussions about adding some kind of event system, but it's not in the spec yet (if it ever will be). There is however a playbackState property on buffer sources that you can check out: https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#AudioBufferSourceNode
Other than that, you best bet is to use timeouts based on the buffer length and run your callback when that fires.
Yep, looks like it's been added: AudioBufferSourceNode.onended https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/onended