Web Audio API:停止播放所有预定的声音

2024-01-08

所以我有一堆加载的音频样本,我在下面的代码中调用调度函数:

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

它工作正常并按预期播放预定的声音。

我该如何停止/取消所有预定的声音播放?

因为现在当我尝试打电话时stop()方法它只会停止播放最后安排的声音。


您需要跟踪您在调度程序中创建的 BufferSource 节点(通过索引引用),然后运行所有节点。例如。:

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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Web Audio API:停止播放所有预定的声音 的相关文章