我正在使用以下方法来播放包含wav数据的字节数组.该函数正在从GWT项目中调用.
此功能播放声音,但它听起来像某种地狱怪物.采样率肯定是正确的(声音是由neospeech生成的)我已经为numberOfSamples尝试了各种值,这似乎只代表音频数据的长度.
numberOfSamples的值大于30000将播放音频文件的全长,但它是乱码且可怕的.
那么,我做错了什么?
function playByteArray(byteArray, numberOfSamples) {
sampleRate = 8000;
if (!window.AudioContext) {
if (!window.webkitAudioContext) {
alert("Your browser does not support any AudioContext and cannot play back this audio.");
return;
}
window.AudioContext = window.webkitAudioContext;
}
var audioContext = new AudioContext();
var buffer = audioContext.createBuffer(1, numberOfSamples, sampleRate);
var buf = buffer.getChannelData(0);
for (i = 0; i < byteArray.length; ++i) {
buf[i] = byteArray[i];
}
var source = audioContext.createBufferSource();
source.buffer = buffer;
source.connect(audioContext.destination);
source.start(0);
}