【问题标题】:Web Audio playing back in Chrome but not Firefox在 Chrome 而不是 Firefox 中播放网络音频
【发布时间】:2014-02-12 20:28:30
【问题描述】:

Firefox 似乎正在处理音频,但它不会播放它。这个确切的代码适用于 Chrome。我没有从控制台看到任何错误,所以我真的很茫然。我认为它与 audioBuffer.getChannelData(0).set(audio);但我不确定。任何人都知道为什么这个音频不会在 FF 中播放,但会在 Chrome 中播放?我现在正在运行 FF 27。

window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var init = 0;
var nextTime = 0;
var audioStack = [];

function toArrayBuffer(buffer) {
    var ab = new ArrayBuffer(buffer.length);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buffer.length; ++i) {
    view[i] = buffer[i];
    }
    return ab;
}

socket.on('stream', function(data) {
    audioStack.push(data);
    //if ((init!=0) || (audioStack.length > 10)) { // make sure we put at least 10 chunks in the buffer before starting
            init++;
            scheduleBuffers();
    //}
});

function scheduleBuffers() {
    while (audioStack.length) {

        var data  = toArrayBuffer(audioStack.shift().data);
        var audio = [];
        audData = new Int16Array(data);
        for (var i = 0; i < audData.length; i++) {
            audio[i] = (audData[i]>0)?audData[i]/32767:audData[i]/32768; // convert buffer to within the range -1.0 -> +1.0
        }

        var source      = context.createBufferSource();
        var audioBuffer = context.createBuffer(1, audio.length , 44100);
        source.buffer   = audioBuffer;

        audioBuffer.getChannelData(0).set(audio);

        source.connect(context.destination);
        if (nextTime == 0)
            nextTime = context.currentTime + 0.05;  /// add 50ms latency to work well across systems - tune this if you like
        source.start(nextTime);
        console.log('length: '+source.buffer.duration);
        nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
    };
}

【问题讨论】:

标签: html firefox audio web-audio-api


【解决方案1】:

看这段代码:

    source.buffer   = audioBuffer;
    audioBuffer.getChannelData(0).set(audio);

你能试着改变这两行的顺序吗?也就是说,先给audioBuffer设置通道数据,然后赋值给source.buffer。这能解决你的问题吗?

【讨论】:

  • 是的,这解决了问题!由于浏览器之间的结果差异,这是否会被视为错误?
  • 在将来也会在 Chrome 中引起问题。这是对规范的一项相对较新的更改,以处理潜在的竞争条件。
  • 是的。这里的问题是,当您在 AudioBufferSourceNode 上设置缓冲区属性时,我们将在赋值右侧使用的 AudioBuffer 对象中的底层数组缓冲区中性化,以使将来无法对数组缓冲区进行修改影响音频线程看到的内容。正如 Chris 所提到的,这样做是为了防止潜在的竞争条件。在设置缓冲区属性后,您的代码依赖于对 AudioBuffer 底层数组缓冲区的修改。希望这会有所帮助。
猜你喜欢
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2016-07-23
相关资源
最近更新 更多