在源节点上调用 stop() 是否会触发结束事件?
Does calling stop() on a source node trigger an ended event?
根据网络音频 API 规范
http://webaudio.github.io/web-audio-api/
我可以分配一个事件处理程序,该处理程序在源节点播放完毕时运行(源节点的 onended
属性)。但是,如果我在音频源节点上调用 stop(0)
,是否会触发该事件?规格似乎并不清楚。
我可以在各种浏览器上尝试这个,但我想知道正确的标准行为。当主动 stop
ped 源节点时,ended
事件会触发吗?还是 ended
事件仅在音频播放完毕时触发?
onended
类型为 EventHandler,
A 属性 用于为分派到 AudioBufferSourceNode 节点类型的结束事件设置 EventHandler(在 HTML[HTML] 中描述)。 当 AudioBufferSourceNode 的缓冲区播放完成时,一个 Event 类型的事件(在 HTML [HTML] 中描述)将被调度到事件处理程序.
它声明它在音频数据结束时触发,或者在停止时触发。
这些行让我感到困惑:
void start (optional double when = 0, optional double offset = 0, optional double duration);
void stop (optional double when = 0);
attribute EventHandler onended;
是的。 onended
音频播放完毕或调用 stop()
时触发事件。
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var button = document.querySelector('button');
var stop = document.querySelector('#stop');
var source;
// Stereo
var channels = 2;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx.sampleRate * 2.0;
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = function () {
// Fill the buffer with white noise;
//just random values between -1.0 and 1.0
for (var channel = 0; channel < channels; channel++) {
// This gives us the actual ArrayBuffer that contains the data
var nowBuffering = myArrayBuffer.getChannelData(channel);
for (var i = 0; i < frameCount; i++) {
// Math.random() is in [0; 1.0]
// audio needs to be in [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = myArrayBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the 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>
根据网络音频 API 规范 http://webaudio.github.io/web-audio-api/
我可以分配一个事件处理程序,该处理程序在源节点播放完毕时运行(源节点的 onended
属性)。但是,如果我在音频源节点上调用 stop(0)
,是否会触发该事件?规格似乎并不清楚。
我可以在各种浏览器上尝试这个,但我想知道正确的标准行为。当主动 stop
ped 源节点时,ended
事件会触发吗?还是 ended
事件仅在音频播放完毕时触发?
onended
类型为 EventHandler,
A 属性 用于为分派到 AudioBufferSourceNode 节点类型的结束事件设置 EventHandler(在 HTML[HTML] 中描述)。 当 AudioBufferSourceNode 的缓冲区播放完成时,一个 Event 类型的事件(在 HTML [HTML] 中描述)将被调度到事件处理程序.
它声明它在音频数据结束时触发,或者在停止时触发。
这些行让我感到困惑:
void start (optional double when = 0, optional double offset = 0, optional double duration);
void stop (optional double when = 0);
attribute EventHandler onended;
是的。 onended
音频播放完毕或调用 stop()
时触发事件。
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var button = document.querySelector('button');
var stop = document.querySelector('#stop');
var source;
// Stereo
var channels = 2;
// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx.sampleRate * 2.0;
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = function () {
// Fill the buffer with white noise;
//just random values between -1.0 and 1.0
for (var channel = 0; channel < channels; channel++) {
// This gives us the actual ArrayBuffer that contains the data
var nowBuffering = myArrayBuffer.getChannelData(channel);
for (var i = 0; i < frameCount; i++) {
// Math.random() is in [0; 1.0]
// audio needs to be in [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = myArrayBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the 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>