W3cubDocs

/DOM

HTMLMediaElement.srcObject

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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 and a File (which inherits from Blob).

As of November 2017, browsers only support MediaStream. For MediaSource, Blob and File, you have to create a URL with URL.createObjectURL() and assign it to HTMLMediaElement.src. See below for an example.

Syntax

var mediaStream = HTMLMediaElement.srcObject
HTMLMediaElement.srcObject = mediaStream

Value

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

Example

This example uses srcObject and gracefully falls back to src if srcObject throws an error.

const mediaSource = new MediaSource();
const video = document.createElement('video');
try {
  video.srcObject = mediaSource;
} catch (error) {
  video.src = URL.createObjectURL(mediaSource);
}

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'srcObject' in that specification.
Living Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 52.0[1] 18.0 (18.0)[1][2]
42.0 (42.0)[1]
? 39[1] 11
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support 52.0 ? ? ? 39 11 52.0

[1] Only MediaStream objects are currently supported. MediaSource, Blob and File objects are yet to be supported, and throw a TypeError. See the tracking bugs for MediaSource support in Chrome (bug 506273) and Firefox (bug 886194).

[2] Early versions of Firefox implemented this under the non-standard name mozSrcObject. This non-standard property was removed in Firefox 58 (bug 1183495).

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject