使用以下服务播放字节数组格式的音频文件(wav/mp3)。
myAudioService.getAudioTone(userid).then(function (data) {
var context; // Audio context
var buf; // Audio buffer
$window.AudioContext = $window.webkitAudioContext;
context = new AudioContext();
$timeout(function () {
$scope.playByteArray = function(){
var arrayBuffer = new ArrayBuffer(data.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < data.length; i++) {
bufferView[i] = data[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play();
});
}
$scope.play = function(audioBuffer){
// Create a source node from the buffer
var source = context.createBufferSource();
source.buffer = buf;
// Connect to the final output node (the speakers)
source.connect(context.destination);
// Play immediately
source.start(0);
}
if(data.length !== '' || data !== ''){
$scope.playByteArray();
}
}, 3000);
});
函数被调用,但抛出以下异常。
未捕获( promise 中)DOMException:无法解码音频数据
如何在 Chrome、FF 和 IE 中运行它?
附注$window
和 $timeout
已在 Controller 中定义。
最佳答案
根据错误消息 arrayBuffer
不包含您认为它包含的内容。您应该验证数组中的字节是否与编码的 wav/mp3 文件相同。
关于javascript - AngularJS - 解码字节数组并播放音频文件(Wav/MP3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42786750/