I tried to use the Screen Recording feature that Twilio provides but found the Composition API hard to work with.
This is the code snippet that I added to my Video Application to implement Screen Recording.
async function StartRecording() {
try {
stream = await navigator.mediaDevices.getDisplayMedia({
video: { mediaSource: "screen" },
});
recorder = new MediaRecorder(stream);
const chunks = [];
recorder.ondataavailable = (e) => chunks.push(e.data);
recorder.onstop = (e) => {
setIsRecord(false);
const completeBlob = new Blob(chunks, { type: chunks[0].type });
const fileReader = new FileReader();
fileReader.onload = function (e) {
let anchor = document.createElement("a");
anchor.href = e.target.result;
anchor.download = `video-${new Date()}`;
anchor.click();
};
fileReader.readAsDataURL(completeBlob);
};
recorder.start();
} catch (err) {
console.log(err);
setIsRecord(false);
}
}
function StopRecording() {
recorder.stop();
stream.getVideoTracks()[0].stop();
}
<span onClick={() => setIsRecord(!isRecord)}>
{!isRecord ? (<div onClick={StartRecording} className='record-on'>
<FiberManualRecordIcon
fontSize='large'
className='videoActionOn'
/>
<p>Start Record</p>
</div>
) : (
<div onClick={StopRecording} className='record-off'>
<HighlightOffIcon
fontSize='large'
className='videoActionOn'
/>
<p>Stop Record</p>
</div>
)}
</span>
Feel Free to leave behind a comment so that I could improve this answer