HTMLMediaElement.srcObject - Web APIs 编辑

The srcObject property of the HTMLMediaElement interface sets or returns the object which serves as the source of the media associated with the HTMLMediaElement. The object can be a MediaStream, a MediaSource, a Blob, or a File (which inherits from Blob).

Note: As of March 2020, only Safari supports setting objects other than MediaStream. Until other browsers catch up, for MediaSource, Blob and File, consider falling back to creating a URL with URL.createObjectURL() and assign it to HTMLMediaElement.src. See below for an example.

Syntax

var sourceObject = HTMLMediaElement.srcObject;

HTMLMediaElement.srcObject = sourceObject;

Value

MediaStreamMediaSource, Blob, or File object (though see the compatibility table for what is actually supported).

Usage notes

Older versions of the Media Source specification required using createObjectURL() to create an object URL then setting src to that URL. Now you can just set srcObject to the MediaStream directly.

Examples

Basic example

In this example, a MediaStream from a camera is assigned to a newly-created <video> element.

const mediaStream = await navigator.mediaDevices.getUserMedia({video: true});
const video = document.createElement('video');
video.srcObject = mediaStream;

In this example, a new MediaSource is assigned to a newly-created <video> element.

const mediaSource = new MediaSource();
const video = document.createElement('video');
video.srcObject = mediaSource;

Supporting fallback to the src property

The examples below support older browser versions that require you to create an object URL and assign it to src if srcObject isn't supported.

First, a MediaStream from a camera is assigned to a newly-created <video> element, with fallback for older browsers.

const mediaStream = await navigator.mediaDevices.getUserMedia({video: true});
const video = document.createElement('video');
if ('srcObject' in video) {
  video.srcObject = mediaStream;
} else {
  // Avoid using this in new browsers, as it is going away.
  video.src = URL.createObjectURL(mediaStream);
}

Second, a new MediaSource is assigned to a newly-created <video> element, with fallback for older browsers and browsers that don't yet support assignment of MediaSource directly.

const mediaSource = new MediaSource();
const video = document.createElement('video');
// Older browsers may not have srcObject
if ('srcObject' in video) {
  try {
    video.srcObject = mediaSource;
  } catch (err) {
    if (err.name != "TypeError") {
      throw err;
    }
    // Even if they do, they may only support MediaStream
    video.src = URL.createObjectURL(mediaSource);
  }
} else {
  video.src = URL.createObjectURL(mediaSource);
}

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'srcObject' in that specification.
Living StandardInitial definition.

Browser compatibility

BCD tables only load in the browser

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

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

发布评论

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

词条统计

浏览:97 次

字数:6191

最后编辑:7 年前

编辑次数:0 次

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