Reputation: 8849
I need to playback aac (adts) streaming from WebSocket, I searched the internet and found aac.js can do it but sounds like delay almost 2~5 seconds.
And I found some people use WebAudio stuff to do it. I try it with below code but decoder return error after some packets (not always but repeat error):
Uncaught (in promise) DOMException: Unable to decode audio data
My sample javascript:
var context = new AudioContext();
var nextStartTime = 0;
var audioWs = new WebSocket("ws://" + hostip + ":8084");
audioWs.binaryType = 'arraybuffer';
audioWs.onopen = function(evt) {
console.log("onOpen");
bexit = 0;
};
audioWs.onclose = function(evt) {
console.log("close");
};
audioWs.onmessage = function(evt) {
var blob = evt.data;
if ( blob.slice == undefined) {
console.log("undefined message");
return;
}
decodeAudio(blob);
};
audioWs.onerror = function(evt) {
console.log("error");
};
var decodeAudio = function(blob) {
context.decodeAudioData(
blob,
function(buffer) {
if (!buffer) {
console.log('error decoding file data: ' + url);
return;
}
console.log("playback buffer");
playBuffer(buffer);
},
function(error) {
console.error("decodeAudioData error", error);
});
};
var playBuffer = function(buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
if(nextStartTime == 0){
nextStartTime = context.currentTime + buffer.duration/2;
}
source.connect(context.destination);
source.start(nextStartTime);
nextStartTime += buffer.duration;
};
I not sure if it's caused by data received from websocket incomming too fast then cause decoder error! How should I fix it?
Or maybe it's not good way to do this task with WebAudio API?
Upvotes: 1
Views: 838
Reputation: 6048
WebAudio's decodeAudioData
can't decode partial blobs. It needs the whole file, in general, because in many encoded formats, the necessary information (sample rates, channels, sample size, etc.) is only at the beginning.
Upvotes: 1