我试图播放来自使用Chrome 51的获取API的无限流。(作为微软PCM,16位,单声道11025赫兹的摄像头音频流)WebAudio流与提取:DOMException:无法解码音频数据
的代码可与MP3音乐文件几乎确定,除了一些小问题,但它不会由于某种原因WAV文件在所有的工作我得到“抛出:DOMException:无法解码音频数据”
的代码是从该调整回答Choppy/inaudible playback with chunked audio through Web Audio API
任何想法,如果它可能使其工作与WAV流?
function play(url) {
var context = new (window.AudioContext || window.webkitAudioContext)();
var audioStack = [];
var nextTime = 0;
fetch(url).then(function(response) {
var reader = response.body.getReader();
function read() {
return reader.read().then(({ value, done })=> {
context.decodeAudioData(value.buffer, function(buffer) {
audioStack.push(buffer);
if (audioStack.length) {
scheduleBuffers();
}
}, function(err) {
console.log("err(decodeAudioData): "+err);
});
if (done) {
console.log('done');
return;
}
read()
});
}
read();
})
function scheduleBuffers() {
while (audioStack.length) {
var buffer = audioStack.shift();
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
if (nextTime == 0)
nextTime = context.currentTime + 0.01; /// add 50ms latency to work well across systems - tune this if you like
source.start(nextTime);
nextTime += source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
};
}
}
只要使用play('/ path/to/mp3')来测试代码。 (服务器需要启用CORS,或者与您的运行脚本位于同一个域中)
为什么不能在Firefox上使用这个代码? –
支持以下标志:https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader#Browser_compatibility – jujule