javascript - Does calling stop() on a source node trigger an ended event? -
according web audio api specs http://webaudio.github.io/web-audio-api/
i can assign event handler runs when source node done playing (the onended
attribute of source node). however, if call stop(0)
on audio source node, event triggered? specs don't seem clear on that.
i can try out on various browsers, want know proper standard behavior this. ended
event fire when source node proactively stop
ped? or ended
event fire if audio finishes playing?
yes does. onended
event gets fired when audio finished playing or when stop()
has been called.
example mdn audiocontext docs
var audioctx = new(window.audiocontext || window.webkitaudiocontext)(); var button = document.queryselector('button'); var stop = document.queryselector('#stop'); var source; // stereo var channels = 2; // create empty 2 second stereo buffer @ // sample rate of audiocontext var framecount = audioctx.samplerate * 2.0; var myarraybuffer = audioctx.createbuffer(2, framecount, audioctx.samplerate); button.onclick = function () { // fill buffer white noise; //just random values between -1.0 , 1.0 (var channel = 0; channel < channels; channel++) { // gives actual arraybuffer contains data var nowbuffering = myarraybuffer.getchanneldata(channel); (var = 0; < framecount; i++) { // math.random() in [0; 1.0] // audio needs in [-1.0; 1.0] nowbuffering[i] = math.random() * 2 - 1; } } // audiobuffersourcenode. // audionode use when want play audiobuffer source = audioctx.createbuffersource(); // set buffer in audiobuffersourcenode source.buffer = myarraybuffer; // connect audiobuffersourcenode // destination can hear sound source.connect(audioctx.destination); // start source playing source.start(); source.onended = function () { alert('ended'); }; }; stop.onclick = function() { source.stop(); };
<h1>audiobuffer example</h1> <button>make white noise</button> <button id="stop">stop()</button>
Comments
Post a Comment