【问题标题】:Web Audio API: Stop all scheduled sounds from playingWeb Audio API:停止播放所有预定的声音
【发布时间】:2017-09-13 05:35:04
【问题描述】:

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

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() 方法时,它只会停止播放最后一个预定的声音。

【问题讨论】:

    标签: javascript web-audio-api


    【解决方案1】:

    您需要跟踪您在调度程序中创建的、由索引引用的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多