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
To export MySQL query results to csv , you usuall...
Table of contents Filters 01.What is 02. How to d...
Today when I was writing a flash advertising code,...
1. To optimize the query, try to avoid full table...
After installing MySQL, you will find that the ro...
This article shares the specific code of jQuery t...
Docker officially recommends that we use port map...
This article uses examples to describe the three ...
This article shares the specific code of videojs+...
Today, when we were learning about the Niu Nan new...
Set the background image for the table header. Yo...
Transaction isolation level settings set global t...
Table of contents 1. Data Manipulation Language (...
At this time, you can use overflow:auto; (when the...
When compiling and installing Nginx, some modules...