How to record the user's screen

[画像:François Beaufort]
François Beaufort

Sharing tabs, windows, and screens is possible on the web platform with the Screen Capture API. The getDisplayMedia() method allows the user to select a screen to capture as a media stream. This stream can then be recorded with the MediaRecorder API or shared with others over the network. The recording can be saved to a local file via the showOpenFilePicker() method.

The example below shows how you can record the user's screen in the WebM format, locally preview on the same page, and save the recording to the user's file system.

letstream;
letrecorder;
shareScreenButton.addEventListener("click",async()=>{
// Prompt the user to share their screen.
stream=awaitnavigator.mediaDevices.getDisplayMedia();
recorder=newMediaRecorder(stream);
// Preview the screen locally.
video.srcObject=stream;
});
stopShareScreenButton.addEventListener("click",()=>{
// Stop the stream.
stream.getTracks().forEach(track=>track.stop());
video.srcObject=null;
});
startRecordButton.addEventListener("click",async()=>{
// For the sake of more legible code, this sample only uses the
// `showSaveFilePicker()` method. In production, you need to
// cater for browsers that don't support this method, as
// outlined in https://web.dev/patterns/files/save-a-file/.
// Prompt the user to choose where to save the recording file.
constsuggestedName="screen-recording.webm";
consthandle=awaitwindow.showSaveFilePicker({suggestedName});
constwritable=awaithandle.createWritable();
// Start recording.
recorder.start();
recorder.addEventListener("dataavailable",async(event)=>{
// Write chunks to the file.
awaitwritable.write(event.data);
if(recorder.state==="inactive"){
// Close the file when the recording stops.
awaitwritable.close();
}
});
});
stopRecordButton.addEventListener("click",()=>{
// Stop the recording.
recorder.stop();
});

Browser support

MediaDevices.getDisplayMedia()

Browser Support

  • Chrome: 72.
  • Edge: 79.
  • Firefox: 66.
  • Safari: 13.

Source

MediaRecorder API

Browser Support

  • Chrome: 47.
  • Edge: 79.
  • Firefox: 25.
  • Safari: 14.1.

Source

File System Access API's showSaveFilePicker()

Browser Support

  • Chrome: 86.
  • Edge: 86.
  • Firefox: not supported.
  • Safari: not supported.

Source

Further reading

Demo

Open demo

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年07月03日 UTC.