13

是否可以将事件侦听器添加到网络音频 api 声音?我一直在寻找声音完成但找不到任何东西的事件或触发器。这是我想象它的工作方式:

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);
4

3 回答 3

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

看一下这个

于 2014-01-16T23:48:50.520 回答
5

不是今天,不是。我知道有关于添加某种事件系统的讨论,但它还没有在规范中(如果有的话)。但是,您可以查看缓冲区源上的playbackState 属性:https ://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#AudioBufferSourceNode

除此之外,您最好的选择是使用基于缓冲区长度的超时,并在触发时运行您的回调。

于 2012-11-29T11:30:58.910 回答
1

是的,看起来它已被添加:AudioBufferSourceNode.onended https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/onended

于 2016-07-15T15:20:29.153 回答