I have a task to send a audio stream chunks to node server and i am able to send them using socket and able to receive them also
Every thing work fine for first chunks but after that it throw error
Uncaught (in promise) DOMException: Failed to execute ‘decodeAudioData’ on ‘BaseAudioContext’: Unable to decode audio data
So to test it i tried to send my mediarecorded chunks to my audio context directly and there also first chunks work fine but after that it gave same error
I read somewhere on stackoverflow that decodeAuidoDaa work for only full audio like mp3 or recorded audio but i am not able to find any solution how to use these chunks without throwing the uppper error
Here is some snapshot of code which i tried to test on frontend
from here i send the chunks
mediaRecorder = new MediaRecorder(mediaStream, { mimeType: 'audio/webm' });
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
console.log('Sending...', event);
chunking.push(event.data)
if (chunking.length > 10) {
let blob = new Blob(chunking, { type: 'audio/webm' });
chunking = []
playAudio(blob)
}
}
};
mediaRecorder.start(100);
function that receive chunks and play audio
async function playAudio(blob) {
const arrayBuffer = await blobToArrayBuffer(blob);
if (!audioContext) {
console.log('Creating new audio context');
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
try {
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
console.log('Decoded audioBuffer:', audioBuffer);
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start(0);
} catch (error) {
console.error('Failed to decode audio data', error);
//console.log('ArrayBuffer content:', new Uint8Array(arrayBuffer));
}
}
function blobToArrayBuffer(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
}
and as it is not brower to brower call so i guess i cann’t use webrtc but if you have knowledge to use webrtc to decode or play these audio chunks which don’t have duration and other metadata like mp3 or recorded audio please help