最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Web Audio API, events? - Stack Overflow

programmeradmin5浏览0评论

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

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);
Share Improve this question asked Nov 29, 2012 at 0:51 fatlinesofcodefatlinesofcode 1,66718 silver badges23 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12
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

发布评论

评论列表(0)

  1. 暂无评论