I'm recording animated canvas (WebGL) and Web Audio API output together as a video using the MediaRecorder JS class. Everything works as expected if some audio is playing, but when not, then I get empty blob when stopping MediaRecorder instance.
Below is part of my code:
const chunks = [];
var recorder = null, captureStream = null;
function dataAvailable(event) {
chunks.push(event.data);
}
function startCapture() {
const streamDestination = audio.context.createMediaStreamDestination();
masterGain.connect(streamDestination);
captureStream = canvas.captureStream();
// This line adds audio to the recording if audio is playing
// but if audio isn't playing at the moment, I get empty chunks
// at the end of recording
captureStream.addTrack(streamDestination.stream.getAudioTracks()[0]);
recorder = new MediaRecorder(captureStream);
recorder.ondataavailable = dataAvailable;
recorder.onstop = exportVideo;
recorder.start();
}
function exportVideo() {
// Here chunks will be empty if Web Audio API
// didn't played anything during recording
new Blob(chunks, {'type': 'video/webm'});
...
}
How to get proper recording when Web Audio is silent?
Using Chrome v92 on Windows 10.