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);
The Web Audio API provides a powerful and versatile system for controlling audio on the Web, allowing developers to choose audio sources, add effects to audio, create audio visualizations, apply spatial effects (such as panning) and much more.
The AudioNode interface is a generic interface for representing an audio processing module.
var 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With