Reputation: 51
I use WebSocket to stream audio file's chunks. I defined a handler for the Socket.onmessage event and I defined an AudioContext. The problem is I lose chunks when I decode audio data. Here is my handler:
var socket = new WebSocket('ws://127.0.0.1:8080');
socket.binaryType = 'arraybuffer';
socket.onmessage = function(msg) {
console.log('received'); // printed 29 times
audioctx.decodeAudioData(msg.data, function(buffer) {
console.log('decoded'); // printed 1 time
});
};
I think it's linked to the asynchronism of the callback function but I don't know how to fix it.
Upvotes: 1
Views: 815
Reputation: 13928
decodeAudioData does not support block-level data; it wants an entire file (MP3 header, e.g., not just a chunk begin). It's not that it's async; it's that (I believe) it's failing to decode, because it only decodes the first block and all subsequent blocks fail.
Until there's a better decoding api with progressive support, you'll have to pull in a JS decoding library.
Upvotes: 2