我想将一个bytearray解码为audiobuffer并使用JS在浏览器中播放我的wav文件。 这里我使用的代码,但点击播放按钮我得到一个例外,即“无法解码音频数据”。
{
if (!window.AudioContext){
if (!window.webkitAudioContext){
alert("Your browser does not support any AudioContext and cannot play back this audio.");
return;
}
}
var context = new AudioContext();
var arr = me.queueStore.getAt(0).data.ByteAudio;
var arrayBuffer = new ArrayBuffer(arr.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < arr.length; i++) {
bufferView[i] = arr[i];
}
context.decodeAudioData(bufferView, function (buffer) {
var source = context.createBufferSource(); // creates a sound source
source.buffer = buffer; // tell the source which sound to play
source.connect(context.destination);
source.start(0);
});
}
答案 0 :(得分:0)
得到了答案,这里是解决方案:使用64位字符串播放媒体文件而不是转换为音频缓冲区:
me.fileString = "data:audio/wav;base64," + data.tonebyteArray;
var audio = new Audio(fileString);
audio.play();