Picture-in-Picture API - Web APIs 编辑

The Picture-in-Picture API allow websites to create a floating video window always on top of other windows so that users may continue consuming media while they interact with other content sites, or applications on their device.

Interfaces

PictureInPictureWindow
Represents the floating video window; contains width and height properties, and an onresize event handler property.

Methods

The Picture-in-Picture API adds methods to the HTMLVideoElement and Document interfaces to allow toggling of the floating video window.

Methods on the HTMLVideoElement interface

HTMLVideoElement.requestPictureInPicture()
Requests that the user agent enters the video into picture-in-picture mode

Methods on the Document interface

Document.exitPictureInPicture()
Requests that the user agent returns the element in picture-in-picture mode back into its original box.

Properties

The Picture-in-Picture API augments the HTMLVideoElement, Document, and DocumentOrShadowRoot interfaces with properties that can be used to determine if the floating video window mode is supported and available, if picture-in-picture mode is currently active, and which video is floating.

Properties on the HTMLVideoElement interface

HTMLVideoElement.autoPictureInPicture
The autoPictureInPicture property will automatically enter and leave the picture-in-picture mode for a video element when the user switches tab and/or applications.
HTMLVideoElement.disablePictureInPicture
The disablePictureInPicture property will provide a hint to the user agent to not suggest the picture-in-picture to users or to request it automatically.

Properties on the Document interface

Document.pictureInPictureEnabled
The pictureInPictureEnabled property tells you whether or not it is possible to engage picture-in-picture mode. This is false if picture-in-picture mode is not available for any reason (e.g. the "picture-in-picture" feature has been disallowed, or picture-in-picture mode is not supported).

Properties on the DocumentOrShadowRoot interface

DocumentOrShadowRoot.pictureInPictureElement
The pictureInPictureElement property tells you which Element is currently being displayed in the floating window. If this is null, the document has no node currently in picture-in-picture mode.

Events

The Picture-in-Picture API defines three events, which can be used to detect when picture-in-picture mode is toggled and when the floating video window is resized.

enterpictureinpicture
Sent to a HTMLVideoElement when it enters picture-in-picture mode. The associated event handler is HTMLVideoElement.onenterpictureinpicture
leavepictureinpicture
Sent to a HTMLVideoElement when it leaves picture-in-picture mode. The associated event handler is HTMLVideoElement.onleavepictureinpicture
resize
Sent to a PictureInPictureWindow when it changes size. The associated event handler is PictureInPictureWindow.onresize

Controlling styling

The :picture-in-picture CSS pseudo-class matches the video element currently in picture-in-picture mode, allowing you to configure your stylesheets to automatically adjust the size, style, or layout of content when a video switches back and forth between picture-in-picture and traditional presentation modes.

Controlling access

The availability of picture-in-picture mode can be controlled using Feature Policy. The full-screen mode feature is identified by the string "picture-in-picture", with a default allow-list value of "self", meaning that picture-in-picture mode is permitted in top-level document contexts, as well as to nested browsing contexts loaded from the same origin as the top-most document.

See Using Feature Policy to learn more about using Feature Policy to control access to an API.

Examples

In this example, a video is presented in a web page. Clicking the button below lets the user toggle the floating video window.

Toggling full-screen mode

This code is called by a click handler when the user clicks the "Toggle Picture-in-Picture" button:

function togglePictureInPicture() {
  if (document.pictureInPictureElement) {
      document.exitPictureInPicture();
  } else {
    if (document.pictureInPictureEnabled) {
      video.requestPictureInPicture();
    }
  }
}

This block starts by looking at the value of the document's pictureInPictureElement attribute. If the value is null, no video is in the floating window. so we can request a video to enter the picture-in-picture mode; otherwise, it's the element that's currently in picture-in-picture mode. Switching to picture-in-picture mode is done by calling HTMLVideoElement.requestPictureInPicture() on the <video> element.

If a video is in the floating window (pictureInPictureElement is not null), we call exitPictureInPicture() on the document to bring the video back into its initial box.

Specifications

SpecificationStatus
Picture-in-Picture APIDraft

Browser compatibility

HTMLVideoElement.requestPictureInPicture

BCD tables only load in the browser

HTMLVideoElement.autoPictureInPicture

BCD tables only load in the browser

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

HTMLVideoElement.disablePictureInPicture

BCD tables only load in the browser

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

PictureInPictureWindow

BCD tables only load in the browser

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:127 次

字数:13679

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文