Html 在Chrome中播放网络音频,但不在Firefox中播放
Firefox似乎正在处理音频,但不会播放。这个精确的代码在Chrome上运行良好。我在控制台上没有看到任何错误,所以我真的很茫然。我认为它与audioBuffer.getChannelData(0.set(audio)有关;但我不确定。有人知道为什么这个音频不会在FF中播放,但会在Chrome中播放吗?我现在正在运行FF 27Html 在Chrome中播放网络音频,但不在Firefox中播放,html,firefox,audio,web-audio-api,Html,Firefox,Audio,Web Audio Api,Firefox似乎正在处理音频,但不会播放。这个精确的代码在Chrome上运行良好。我在控制台上没有看到任何错误,所以我真的很茫然。我认为它与audioBuffer.getChannelData(0.set(audio)有关;但我不确定。有人知道为什么这个音频不会在FF中播放,但会在Chrome中播放吗?我现在正在运行FF 27 window.AudioContext = window.AudioContext || window.webkitAudioContext; var context
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
};
}
window.AudioContext=window.AudioContext | | window.webkitadiocontext;
var context=新的AudioContext();
var init=0;
var-nextTime=0;
var audioStack=[];
函数到ArrayBuffer(缓冲区){
var ab=新的数组缓冲区(buffer.length);
var视图=新的UINT8阵列(ab);
对于(变量i=0;i10)){//请确保在开始之前至少在缓冲区中放入10个块
init++;
调度缓冲区();
//}
});
函数scheduleBuffers(){
while(audioStack.length){
var data=toArrayBuffer(audioStack.shift().data);
var audio=[];
audData=新的INT16阵列(数据);
对于(var i=0;i0)?audData[i]/32767:audData[i]/32768;//将缓冲区转换为-1.0->+1.0范围内的缓冲区
}
var source=context.createBufferSource();
var audioBuffer=context.createBuffer(1,audio.length,44100);
source.buffer=音频缓冲区;
audioBuffer.getChannelData(0).set(音频);
source.connect(context.destination);
如果(nextTime==0)
nextTime=context.currentTime+0.05;///添加50ms延迟以跨系统正常工作-如果愿意,请对此进行调优
source.start(下一时刻);
log('length:'+source.buffer.duration);
nextTime+=source.buffer.duration;//使下一个缓冲区在播放前等待上一个缓冲区的长度
};
}
查看此代码:
source.buffer = audioBuffer;
audioBuffer.getChannelData(0).set(audio);
你能试着改变这两行的顺序吗?换句话说,首先为audioBuffer设置通道数据,然后将其分配给source.buffer。这能帮你解决问题吗?你介意链接到显示问题的完整网页吗?给你:是的,这能解决问题!由于浏览器之间的结果不同,这会被认为是一个bug吗?这也会在将来的Chrome中引起问题。这是一个相对较新的规范变更,以应对潜在的竞争条件。是的。这里的问题是,当您在AudioBufferSourceNode上设置buffer属性时,我们将在分配的右边符号处使用的AudioBuffer对象中对底层数组缓冲区进行中性化,以使以后对数组缓冲区的修改不可能影响音频线程看到的内容。正如Chris提到的,这样做是为了防止潜在的比赛条件。设置buffer属性后,您的代码依赖于对AudioBuffer的底层数组缓冲区的修改。希望这有帮助。