此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
屏幕捕捉 API
屏幕捕获 API 对现有的媒体捕获和流 API 进行了补充,让用户选择一个屏幕或屏幕的一部分(如一个窗口)作为媒体流进行捕获。然后,该流可以被记录或通过网络与他人共享。
In this article
屏幕捕捉 API 的概念和用法
屏幕捕捉 API 使用起来相对简单。其唯一的方法是MediaDevices.getDisplayMedia(),它的任务是以捕获的形式要求用户选择一个屏幕或屏幕的一部分的MediaStream。
要开始从屏幕上捕捉视频,你需要在getDisplayMedia() 的实例上调用Medianavigator.mediaDevices:
captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);ThePromise returned bygetDisplayMedia() resolves to aMediaStream which streams the captured media.
See the articleUsing the Screen Capture API for a more in-depth look at how to use the API to capture screen contents as a stream.
增加现有接口
屏幕捕捉 API 没有自己的任何接口,而是在现有的MediaDevices 接口上添加了一个方法。
MediaDevices 接口
MediaDevices.getDisplayMedia()The
getDisplayMedia()method is added to theMediaDevicesinterface. Similar togetUserMedia(), this method creates a promise that resolves with aMediaStreamcontaining the display area selected by the user, in a format that matches the specified options.
Additions to existing dictionaries
The Screen Capture API adds properties to the following dictionaries defined by other specifications.
MediaTrackConstraints
MediaTrackConstraints.cursorA
ConstrainDOMStringindicating whether or not the cursor should be included in the captured display surface's stream, and if it should always be visible or if it should only be visible while the mouse is in motion.MediaTrackConstraints.displaySurfaceA
ConstrainDOMStringindicating what type of display surface is to be captured. The value is one ofapplication,browser,monitor, orwindow.MediaTrackConstraints.logicalSurfaceIndicates whether or not the video in the stream represents a logical display surface (that is, one which may not be entirely visible onscreen, or may be completely offscreen). A value of
trueindicates a logical display surface is to be captured.
MediaTrackSettings
MediaTrackSettings.cursorA string which indicates whether or not the display surface currently being captured includes the mouse cursor, and if so, whether it's only visible while the mouse is in motion or if it's always visible. The value is one of
always,motion, ornever.MediaTrackSettings.displaySurfaceA string indicating what type of display surface is currently being captured. The value is one of
application,browser,monitor, orwindow.MediaTrackSettings.logicalSurfaceA Boolean value which is
trueif the video being captured doesn't directly correspond to a single onscreen display area.
MediaTrackSupportedConstraints
MediaTrackSupportedConstraints.cursorA Boolean which is
trueif the user agent and device support theMediaTrackConstraints.cursorconstraint.MediaTrackSupportedConstraints.displaySurfaceA Boolean which is
trueif the current environment supports theMediaTrackConstraints.displaySurfaceconstraint.MediaTrackSupportedConstraints.logicalSurfaceA Boolean which is
trueif the current environment supports the constraintMediaTrackConstraints.logicalSurface.
Dictionaries
The following dictionaries are defined by the Screen Capture API.
CursorCaptureConstraintAn enumerated string type used to provide the value for the
cursorproperty for the settings and constraints. The possible values arealways,motion, andnever.DisplayCaptureSurfaceTypeAn enumerated string type which is used to identify the kind of display surface to capture. This type is used for the
displaySurfaceproperty in the constraints and settings objects, and has the possible valuesapplication,browser,monitor, andwindow.
Feature Policy validation
User agents that support Feature Policy (either using HTTP'sPermissions-Policy header or the<iframe> attributeallow) can specify a desire to use the Screen Capture API using the policy control directivedisplay-capture:
<iframe allow="display-capture" src="/some-other-document.html"></iframe>The default allow list isself, which lets the any content within the document use Screen Capture.
SeeUsing Feature Policy for a more in-depth explanation of how Feature Policy is used.
规范
| Specification |
|---|
| Screen Capture> |
| Element Capture> |
| Region Capture> |
| Captured Surface Control> |