I've got a very weird issue with the MediaRecorder API which I haven't been able to fix the last two days.
This simplified sample works perfectly fine, when I click the Chrome UI "Stop Sharing" button, everything stops appropriately:
desktopStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true,
});
let rec = new MediaRecorder(desktopStream, {
mimeType: "video/webm; codecs=vp8,opus",
});
rec.onstop = async () => {
desktopStream.getTracks().forEach((s) => s.stop());
desktopStream = null;
//blobs.push(MediaRecorder.requestData());
blob = new Blob(blobs, {
type: "video/webm",
});
};
However this doesn't, it keeps recording (or some MediaTracks keep running, not entirely sure):
desktopStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true,
});
voiceStream = await navigator.mediaDevices.getUserMedia({
video: false,
audio: true,
});
// this is the culprit, when commented everything works as expected
desktopStream.addTrack(voiceStream.getAudioTracks()[0]);
let rec = new MediaRecorder(desktopStream, {
mimeType: "video/webm; codecs=vp8,opus",
});
rec.onstop = async () => {
//explicitly stop tracks
desktopStream.getTracks().forEach((s) => s.stop();
voiceStream.getTracks().forEach((s) => s.stop());
desktopStream = null;
voiceStream = null;
//blobs.push(MediaRecorder.requestData());
blob = new Blob(blobs, {
type: "video/webm",
});
};
The second code block results in chrome still showing this red recording icon. I've read many times that you need to stop the tracks manually in a forEach loop, which I do, but it never gets to that, the recorder.onstop function doesn't seem to get called.
Here's the complete fiddle: https://jsfiddle.net/agcty/xhb0c4o3/19/
Steps to reproduce:
- Click "Start Recording"
- Select Screen
- Do anything for some seconds
- Click the Chrome "Stop Sharing" button, not the button in the fiddle
- The red recording icon will still show and you can't download the recording when clicking "Download"
- Click "Stop Recording" (the button in the fiddle), now the recording is properly stopped
- Now comment the line "desktopStream.addTrack(voiceStream.getAudioTracks()[0]);"
- Everything works

