I'm trying to record and play audio in html using MediaSourceExtensions and MediaRecorder.
here is my code:
async function recordAudio()
{
let mediaDevices = navigator.mediaDevices;
let MediaStream = await mediaDevices.getUserMedia({audio: true});
let mediaRecorder = new MediaRecorder(MediaStream);
let audio = document.querySelector('audio');
let chunks = [];
mediaRecorder.start(400);
var mediaSource = new MediaSource();
audio.src = URL.createObjectURL(mediaSource);
var sourceBuffer = mediaSource.addSourceBuffer("audio/mp4; codecs="mp4a.40.2"");
mediaRecorder.ondataavailable = (e) =>
{
sourceBuffer.appendSourceBuffer(e.data);
};
}
here are the errors I'm getting(on Firefox):
Uncaught (in promise) DOMException: An attempt was made to use an object that is not, or is no longer, usable (line 11).
what I'm going for is for the voice to be played almost immediately/400ms after the input but I have no idea how to fix it.
Any help would be much appreciated.