解码我有一个网页,其解码波形文件的某些原因。 Chrome和Safari似乎工作正常。 Firefox偶尔无法解码文件并出现错误:“传递给decodeAudioData的缓冲区包含无法成功解码的无效内容。”我创建了一个jsfiddle这说明了问题:为什么某些.wav文件无法在Firefox
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var source;
function getData() {
source = audioCtx.createBufferSource();
request = new XMLHttpRequest();
request.open('GET', 'https://mpclubtest.s3.amazonaws.com/Malice_Bass.wav', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
source.buffer = buffer;
source.connect(audioCtx.destination);
},
function(e){"Error with decoding audio data" + e.err});
}
request.send();
}
getData();
source.start(0);
谁能告诉我是什么问题,如果有任何的方式来规避?非常感谢。
编辑 感谢迈克尔·查尼的重大贡献,我能够实现一些JavaScript哪些进程,以便它可以在Firefox中播放的浪潮。代码修剪16字节的“fmt”块的任何部分。代码位于:jfiddle
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var source;
function getData() {
source = audioCtx.createBufferSource();
request = new XMLHttpRequest();
request.open('GET', 'https://mpclubtest.s3.amazonaws.com/Malice_Bass.wav', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
var dv = new DataView(audioData);
var junk = 0;
var position = 12;
do {
var header = String.fromCharCode.apply(null, Uint8Array(audioData, position, 4));
var length = dv.getUint32(position + 4, true);
if (header.trim() === 'fmt') {
junk = junk + length - 16;
}
position = position + 8 + length;
}while(position < audioData.byteLength);
var productArray = new Uint8Array(audioData.byteLength - junk);
productArray.set(new Uint8Array(audioData, 0, 12));
var newPosition = 12;
position = 12;
var fmt_length_spot;
do {
var header = String.fromCharCode.apply(null, Uint8Array(audioData, position, 4));
var length = dv.getUint32(position + 4, true);
if (header.trim() === 'fmt') {
productArray.set(new Uint8Array(audioData, position, 24), newPosition);
fmt_length_spot = newPosition + 4;
newPosition = newPosition + 24;
}
else {
productArray.set(new Uint8Array(audioData, position, length + 8), newPosition);
newPosition = newPosition + 8 + length;
}
position = position + 8 + length;
}while(position < audioData.byteLength);
audioData = productArray.buffer;
dv = new DataView(audioData);
dv.setUint32(4, audioData.byteLength - 8, true);
dv.setUint32(fmt_length_spot, 16, true);
audioCtx.decodeAudioData(audioData, function(buffer) {
source.buffer = buffer;
source.connect(audioCtx.destination);
},
function(e){"Error with decoding audio data" + e.err});
}
request.send();
}
getData();
source.start(0);
谢谢迈克尔。
你介意我这个文件添加到我的WebAudio解码器的测试套件?这个项目背后的想法是不断检查哪些浏览器在解码什么样的音频文件时失败? http://notthetup.github.io/decodethis/ – notthetup 2014-10-09 02:54:18
@notthetup,它的全部属于你的。 – laertiades 2014-10-09 03:30:48
我试过这段代码,它并没有解决问题(我使用Firefox 52.0.2)。这并不令人意外,因为它已经在3年前写下了。 – maximedupre 2017-07-07 01:55:47