The Screen Capture API introduces additions to the existing Media Capture and Streams API to let the user select a screen or portion of a screen (such as a window) to capture as a media stream. This stream can then be recorded or shared with others over the network.
The Screen Capture API is relatively simple to use. Its sole method is
MediaDevices.getDisplayMedia()
, whose job is to ask the user to select a screen or portion of a screen to capture in the form of a
MediaStream
.
To start capturing video from the screen, you call
getDisplayMedia()
on the instance of
Media
navigator.mediaDevices
:
captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
Promise
返回通过
getDisplayMedia()
resolves to a
MediaStream
which streams the captured media.
See the article Using the Screen Capture API for a more in-depth look at how to use the API to capture screen contents as a stream.
The Screen Capture API doesn't have any interfaces of its own; instead, it adds one method to the existing
MediaDevices
接口。
MediaDevices.getDisplayMedia()
getDisplayMedia()
method is added to the
MediaDevices
interface. Similar to
getUserMedia()
, this method creates a promise that resolves with a
MediaStream
containing the display area selected by the user, in a format that matches the specified options.
The Screen Capture API adds properties to the following dictionaries defined by other specifications.
MediaTrackConstraints.cursor
ConstrainDOMString
indicating 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.displaySurface
ConstrainDOMString
indicating what type of display surface is to be captured. The value is one of
application
,
浏览器
,
monitor
,或
window
.
MediaTrackConstraints.logicalSurface
true
indicates a logical display surface is to be captured.
MediaTrackSettings.cursor
always
,
motion
,或
never
.
MediaTrackSettings.displaySurface
application
,
浏览器
,
monitor
,或
window
.
MediaTrackSettings.logicalSurface
true
if the video being captured doesn't directly correspond to a single onscreen display area.
MediaTrackSupportedConstraints.cursor
true
if the user agent and device support the
MediaTrackConstraints.cursor
constraint.
MediaTrackSupportedConstraints.displaySurface
true
if the current environment supports the
MediaTrackConstraints.displaySurface
constraint.
MediaTrackSupportedConstraints.logicalSurface
true
if the current environment supports the constraint
MediaTrackConstraints.logicalSurface
.
The following dictionaries are defined by the Screen Capture API.
CursorCaptureConstraint
cursor
property for the settings and constraints. The possible values are
always
,
motion
,和
never
.
DisplayCaptureSurfaceType
displaySurface
property in the constraints and settings objects, and has the possible values
application
,
浏览器
,
monitor
,和
window
.
User agents
that support Feature Policy (either using HTTP's
Feature-Policy
header or the
<iframe>
属性
allow
) can specify a desire to use the Screen Capture API using the policy control directive
display-capture
:
<iframe allow="display-capture" src="/some-other-document.html">
The default allow list is
self
, which lets the any content within the document use Screen Capture.
见 Using Feature Policy for a more in-depth explanation of how Feature Policy is used.
| 规范 | 状态 | 注释 |
|---|---|---|
| Screen Capture | 工作草案 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getDisplayMedia()
|
Chrome
72
|
Edge
79
|
Firefox
66
|
IE 不支持 No |
Opera
60
|
Safari 13 |
WebView Android
不支持
No
注意事项
|
Chrome Android 不支持 No |
Firefox Android
不支持
No
注意事项
|
Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
| Audio capture support |
Chrome
74
注意事项
|
Edge
≤79
注意事项
|
Firefox 不支持 No | IE 不支持 No | Opera ? | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
不支持
兼容性未知
见实现注意事项。
用户必须明确启用此特征。
MediaTrackConstraints.cursor
MediaTrackConstraints.displaySurface
MediaTrackConstraints.logicalSurface
MediaTrackSettings.cursor
MediaTrackSettings.displaySurface
MediaTrackSettings.logicalSurface
MediaTrackSupportedConstraints.cursor
MediaTrackSupportedConstraints.displaySurface
MediaTrackSupportedConstraints.logicalSurface