mardi 1 décembre 2020

Uncaught UnknownError: Failed to execute 'start' on 'MediaRecorder': There was an error starting the MediaRecorder

I am working on a chrome extension, the recorder works fine if I run it for the first time, but when the recording is once stopped and I try to record for the second time then it throws the error:

Uncaught UnknownError: Failed to execute 'start' on 'MediaRecorder': There was an error starting the MediaRecorder.

Here is my code:

function getTabAudio() {
  chrome.tabCapture.capture(constraints, (_stream) => {
    // keep playing the audio in the background
    const audio = new Audio();
    audio.srcObject = _stream;
    audio.play();

    tabStream = _stream;
    tabAudio = audioContext.createMediaStreamSource(tabStream);
    tabAudio.connect(destination);

    output = new MediaStream();
    output.addTrack(destination.stream.getAudioTracks()[0]);

    recorder = new MediaRecorder(output);

    recorder.start();

    recorder.ondataavailable = (e) => {
      chunks.push(e.data);
      // call download when recorder state is inactive
      if (recorder.state == 'inactive') download();
    };
  });
}

// get mic audio
function getMicAudio() {
  navigator.mediaDevices.getUserMedia(constraints).then((mic) => {
    micStream = mic;
    micAudio = audioContext.createMediaStreamSource(micStream);
    micAudio.connect(destination);

    // get tab audio
    getTabAudio();
  });
}

// start recording the stream
function startRecord() {
  getMicAudio();
}

// stop record -> stop all the tracks
function stopRecord() {
  recorder.stop();

  micStream.getTracks().forEach((t) => t.stop());
  tabStream.getTracks().forEach((t) => t.stop());
  output.getTracks().forEach((t) => t.stop());

  micable = true;
  chunks = [];
}

Any help is highly appreciated! Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire