javascriptweb-mediarecorder

MediaRecorder Web API: how to pause/resume Audio Stream (Mic)


I am recording from mic using this:

const micStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
const audioContext = new AudioContext();
const audioDestination = audioContext.createMediaStreamDestination();
const userAudio = audioContext.createMediaStreamSource(micStream);
userAudio.connect(audioDestination);
const tracks = audioDestination.stream.getTracks();
const fullStream = new MediaStream(tracks);
const mediaRecorder = new MediaRecorder(fullStream);

recordedChunks = [];

mediaRecorder.ondataavailable = function (e) {
  recordedChunks.push(e.data);
};

[... creating a Blob and uploading it ...]

I would like to implement a pause/resume button. But I don't see any pause/resume method in any of these classes:

How can I implement a mic pause/resume functionality? (even if it is just muting the mic)


Solution

  • To implement a way for users to mute and unmute a track, use the enabled property. When a track is disabled by setting enabled to false, it generates only empty frames (audio frames in which every sample is 0, or video frames in which every pixel is black).