组合一组实时音频流并将其作为一个音频流播放(两个流之间没有裂缝)

Combine a array of live audio streams and play it as one audio stream(Without Craks between two streams)

我有一个数组列表(实时音频流 5 分钟剪辑),我想将它合并为一个文件并按照数组的顺序在播放器中播放。

function init() {
  // Fix up prefixing
  window.AudioContext = window.AudioContext || window.webkitAudioContext;
  context = new AudioContext();

  bufferLoader = new BufferLoader(
    context,
    [
      audio stream 1,
      audio stream 2,
      audio stream 3
    ],
    finishedLoading
    );

  bufferLoader.load();
}

这是合并多个流的最佳方式。

 function play() {
      //end of stream has been reached
      if (audiobuffer.length === 0) { return; }
      let source = context.createBufferSource();

      //get the latest buffer that should play next
      source.buffer = audiobuffer.shift();
      source.connect(context.destination);

      //add this function as a callback to play next buffer
      //when current buffer has reached its end 
      source.onended = play;
      source.start();
    }

您可以使用 AudioBufferSourceNode.startwhen 参数来安排特定时间播放音频节点。

对于您的示例,您需要创建所有节点,然后安排它们一个接一个地启动。

function play() {
    let totalTime = 0;

    audioBuffers.forEach(buffer => {
        const source = context.createBufferSource();
        source.buffer = buffer
        source.connect(context.destination);
        source.start(context.currentTime + totalTime);
        totalTime += source.duration;
    });
}