First, create an The content is as follows: <!DOCTYPE html> <html> <head> <title>Parcel Sandbox</title> <meta charset="UTF-8" /> </head> <body> <video class="video" width="600px" controls></video> <button class="record-btn">record</button> <script src="./index.js"></script> </body> </html> Then create Listen for button clicks: let btn = document.querySelector(".record-btn"); btn.addEventListener("click", function () { console.log("hello"); }); Open the Now remove the print and replace it with the following: let btn = document.querySelector(".record-btn"); btn.addEventListener("click", async function () { let stream = await navigator.mediaDevices.getDisplayMedia({ video: true }); }); Now click the button and a screen selection box will pop up: Since I am using two screens now, two options will appear. Now you might think that selecting a screen and clicking Share will start the recording. No, this is more complicated than we think. We are going to use let btn = document.querySelector(".record-btn") btn.addEventListener("click", async function () { let stream = await navigator.mediaDevices.getDisplayMedia({ video: true }) // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm" let mediaRecorder = new MediaRecorder(stream, { mimeType: mime }) //Must start mediaRecorder.start() manually }) When our screen is recorded, let btn = document.querySelector(".record-btn") btn.addEventListener("click", async function () { let stream = await navigator.mediaDevices.getDisplayMedia({ video: true }) // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm" let mediaRecorder = new MediaRecorder(stream, { mimeType: mime }) let chunks = [] mediaRecorder.addEventListener('dataavailable', function(e) { chunks.push(e.data) }) //Must start mediaRecorder.start() manually }) Now, when we click the stop sharing button, we want to play the recorded video in our let btn = document.querySelector(".record-btn") btn.addEventListener("click", async function () { let stream = await navigator.mediaDevices.getDisplayMedia({ video: true }) // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm" let mediaRecorder = new MediaRecorder(stream, { mimeType: mime }) let chunks = [] mediaRecorder.addEventListener('dataavailable', function(e) { chunks.push(e.data) }) mediaRecorder.addEventListener('stop', function(){ let blob = new Blob(chunks, { type: chunks[0].type }) let video = document.querySelector(".video") video.src = URL.createObjectURL(blob) }) //Must start mediaRecorder.start() manually }) Now it is basically done and can be polished, such as automatically downloading the recorded video. You can do this: let btn = document.querySelector(".record-btn") btn.addEventListener("click", async function () { let stream = await navigator.mediaDevices.getDisplayMedia({ video: true }) // Need better browser support const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm" let mediaRecorder = new MediaRecorder(stream, { mimeType: mime }) let chunks = [] mediaRecorder.addEventListener('dataavailable', function(e) { chunks.push(e.data) }) mediaRecorder.addEventListener('stop', function(){ let blob = new Blob(chunks, { type: chunks[0].type }) let url = URL.createObjectURL(blob) let video = document.querySelector("video") video.src = url let a = document.createElement('a') a.href = url a.download = 'video.webm' a.click() }) //Must start mediaRecorder.start() manually }) Now, the most basic recording function is complete, let's try it out!! This is the end of this article about using JS to create a screen recording function. For more information about using JS to create a screen recording function, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to add configuration options to Discuz! Forum
>>: A brief discussion on how to set CSS position absolute relative to the parent element
Create a project directory mkdir php Create the f...
What is keepalive? In normal development, some co...
1. Video tag Supports automatic playback in Firef...
Table of contents background Solution 1 Ideas: Co...
WEB development mainly consists of two interactio...
We live in a visual world and are surrounded by m...
To split a string into an array, you need to use ...
Here is a text scrolling effect implemented with ...
version: centos==7.2 jdk==1.8 confluence==6.15.4 ...
<!--[if IE 6]> Only IE6 can recognize <![...
1. Javascript returns to the previous page history...
introduce Usually a background server program mus...
This article shares the data display code for Jav...
This article mainly introduces 6 solutions to the...