--- title: 屏幕捕捉API slug: Web/API/Screen_Capture_API tags: - API - Capture - Conference - Media - MediaDevices - MediaStream - NeedsTranslation - Overview - Screen Capture - Screen Capture API - Screen Sharing - Sharing - TopicStub - Video - Window - display - getDisplayMedia - screen translation_of: Web/API/Screen_Capture_API ---
屏幕捕获API对现有的媒体捕获和流API进行了补充,让用户选择一个屏幕或屏幕的一部分(如一个窗口)作为媒体流进行捕获。然后,该流可以被记录或通过网络与他人共享。
屏幕捕捉API使用起来相对简单。其唯一的方法是{{domxref("MediaDevices.getDisplayMedia()")}},它的任务是以捕获的形式要求用户选择一个屏幕或屏幕的一部分的{{domxref("MediaStream")}}。
要开始从屏幕上捕捉视频,你需要在 getDisplayMedia() 的实例上调用 Media navigator.mediaDevices:
captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
The {{jsxref("Promise")}} returned by getDisplayMedia() resolves to a {{domxref("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.
屏幕捕捉API没有自己的任何接口,而是在现有的 {{domxref("MediaDevices")}} 接口上添加了一个方法。
getDisplayMedia() method is added to the MediaDevices interface. Similar to {{domxref("MediaDevices.getUserMedia", "getUserMedia()")}}, this method creates a promise that resolves with a {{domxref("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.
application, browser, monitor, or window.true indicates a logical display surface is to be captured.always, motion, or never.application, browser, monitor, or window.true if the video being captured doesn't directly correspond to a single onscreen display area.true if the user agent and device support the {{domxref("MediaTrackConstraints.cursor")}} constraint.true if the current environment supports the {{domxref("MediaTrackConstraints.displaySurface")}} constraint.true if the current environment supports the constraint {{domxref("MediaTrackConstraints.logicalSurface")}}.The following dictionaries are defined by the Screen Capture API.
CursorCaptureConstraintcursor property for the settings and constraints. The possible values are always, motion, and never.DisplayCaptureSurfaceTypedisplaySurface property in the constraints and settings objects, and has the possible values application, browser, monitor, and window.{{Glossary("User agent", "User agents")}} that support Feature Policy (either using HTTP's {{HTTPHeader("Feature-Policy")}} header or the {{HTMLElement("iframe")}} attribute {{htmlattrxref("allow", "iframe")}}) 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.
See Using Feature Policy for a more in-depth explanation of how Feature Policy is used.
规范
| 规范 | 状态 | 说明 |
|---|---|---|
| {{SpecName('Screen Capture')}} | {{Spec2('Screen Capture')}} | 初始定义。 |
{{Compat("api.MediaDevices.getDisplayMedia")}}
相关链接