1

是否可以使用 HTML5 实现低延迟音频播放?我目前正在使用AudioContextAPI。但是,我得到大约 4 秒的延迟。这对我的用例来说意义重大。

    if (!window.audioContextInstance) {
      window.audioContextInstance = new webkitAudioContext();
    }
    var context = window.audioContextInstance;

    context.sampleRate = 48000;

    var buffers = [];

    var src = new Float32Array();
    var srcIdx = 0;
    var bufferSize = 2048;

    var sourceNode = context.createScriptProcessor(bufferSize, 1, 2);
    sourceNode.onaudioprocess = function(evt) {
      var c0 = evt.outputBuffer.getChannelData(0);
      var c1 = evt.outputBuffer.getChannelData(1);

      var sample = 0;

      while(sample < bufferSize) {
        if (srcIdx >= src.length) {
          if (!buffers.length) {
            console.log("Warning: Audio Buffer Underflow")
            return;
          }
          src = buffers.shift();
          srcIdx = 0;
        }

        while(sample < bufferSize && srcIdx < src.length) {
          c0[sample] = src[srcIdx++];
          c1[sample] = src[srcIdx++];
          sample++;
        }
      }
    };

    scope.$on('frame', function (event, frame) {          

      while (buffers.length > 1) {
        buffers.shift();
      }

        buffers.push(new Float32Array(frame.data));

      if (buffers.length > 0) {
        sourceNode.connect(context.destination);
      }
    });
  }
4

1 回答 1

1

您可能对riffwave.js感兴趣,它的延迟似乎远低于 4 秒。

于 2014-04-08T14:38:25.817 回答