我正在尝试使用 Chrome 51 播放来自 fetch API 的无限流。(网络摄像头音频流为 Microsoft PCM,16 位,单声道 11025 Hz)
该代码对于 mp3 文件几乎可以正常工作,除了一些小故障,但它对于 wav 文件根本不起作用,由于某种原因,我得到“DOMException:无法解码音频数据”
该代码改编自这个答案通过 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,或者与运行脚本位于同一域)
AudioContext.decodeAudioData 只是不是为了解码部分文件而设计的;它适用于“短”(但完整)的文件。由于 MP3 的分块设计,它有时适用于 MP3 流,但不适用于 WAV 文件。在这种情况下,您需要实现自己的解码器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)