Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Audio API, events?

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);
like image 259
fatlinesofcode Avatar asked Nov 29 '12 00:11

fatlinesofcode


People also ask

What does Web Audio API do?

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.

Which of the following options best describe AudioNode?

The AudioNode interface is a generic interface for representing an audio processing module.


3 Answers

var isFinished = false;
var source = context.createBufferSource();
source.onended = onEnded;
function onEnded() {
    isFinished = true;
    console.log('playback finished');
}

Check this out

like image 56
user3204672 Avatar answered Nov 15 '22 01:11

user3204672


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.

like image 30
Oskar Eriksson Avatar answered Nov 15 '22 01:11

Oskar Eriksson


Yep, looks like it's been added: AudioBufferSourceNode.onended https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/onended

like image 1
vladvlad Avatar answered Nov 15 '22 00:11

vladvlad