This article will show you how to implement a simple screen recorder with JS.
Create a button.
start recording
Write j**ascript
var recording_ongoing = false;var recordingtoggle = document.getelementbyid("recording-toggle");button recordingtoggleaddeventlistener("click", function() else })
It may seem like a lot of content, but in reality, it's just adding an event listener to the button to start and stop recording and change the text accordingly.
Before writing a functional function, declare 3 global variables (outside of the function).
var blob, mediarecorder = null;var chunks =
Now, start the screen recording.
async function startrecording(),audio: true} )devicerecorder = new devicerecorder(stream, )
Create a flow outside of the user's screen. **The logger has a mimetypeThis is the type of output file you want.
You can read more about mimetypes here.
Edge supports the Video Webmmime type. This is the file extension. webm。You can check if your browser supports mimetype in the following way:
console.log(mediarecorder.istypesupported("video/webm"))console.log(mediarecorder.istypesupported("video/mp4"))console.log(mediarecorder.istypesupported("video/mp4;codecs=**c1"))
Add a few lines of startrecording to the function
devicerecorder.ondata**ailable = (e) => devicerecorder.onstop = ()=> devicerecorder.start(250)
Whenever there is data, it is added to the block array (previously defined). When recording is stopped, the stoprecording() function is called.
function stoprecording())chunks = reset data-block var datadownloadurl = urlcreateobjecturl(blob);Bring it to the user's device let a = documentcreateelement('a') a.href = datadownloadurl; a.download = `$webm` a.click() url.revokeobjecturl(datadownloadurl)}
It's that easy to do screen recording with JS. If you want MP4 or other formats, you'll have to convert using the API or do it yourself.
The browser notifies you if your screen is being shared.
Author: Zhang Jie, JD Insurance.*:JD Cloud Developer Community **Please indicate**.