Web Audio API:停止播放所有预定的声音 [英] Web Audio API: Stop all scheduled sounds from playing
本文介绍了Web Audio API:停止播放所有预定的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一堆加载的音频样本,我在下面的代码中调用调度函数:
So i have a bunch of loaded audio samples that I am calling the schedule function with in the code below:
let audio;
function playChannel() {
let audioStart = context.currentTime;
let next = 0;
for(let i = 0; i < 8; i++) {
scheduler(audioStart, next);
next++;
}
}
这是音频调度器功能:
function scheduler(audioStart, index) {
audio = context.createBufferSource();
audio.buffer = audioSamples[index]; //array with all the loaded audio
audio.connect(context.destination);
audio.start(audioStart + (audio.buffer.duration * index));
}
而且它工作正常并按预期播放预定的声音.
And it's working fine and plays the scheduled sounds as it should.
我应该如何停止/取消播放所有预定的声音?
How am I supposed to stop/cancel all the scheduled sounds from playing?
因为现在当我尝试调用 stop()
方法时,它只会停止播放最后一个预定的声音.
Because right now when I try to call the stop()
method it will only stop the last scheduled sound from playing.
推荐答案
您需要跟踪您在调度程序中创建的 BufferSource 节点,由索引引用,然后遍历所有节点.例如:
You'll need to keep track of the BufferSource nodes you're creating inside scheduler, referenced by index, and then run through all of them. E.g.:
var sources = [];
function scheduler(audioStart, index) {
audio = context.createBufferSource();
sources[index] = audio;
audio.buffer = audioSamples[index]; //array with all the loaded audio
audio.connect(context.destination);
audio.start(audioStart + (audio.buffer.duration * index));
}
function stopAll() {
for(let i = 0; i < 8; i++)
if (sources[i])
sources[i].stop(0);
}
这篇关于Web Audio API:停止播放所有预定的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文