Web/API/HTMLMediaElement/srcObject

From Get docs


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

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

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
srcObject

Chrome Partial support 52

Notes'

Partial support 52

Notes'

Notes' Currently only supports MediaStream objects.

Edge Partial support 12

Notes'

Partial support 12

Notes'

Notes' Currently only supports MediaStream objects.

Firefox Partial support 42

Notes'

Partial support 42

Notes'

Notes' Currently only supports MediaStream objects. No support 18 — 58

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

No support No

Opera Partial support 39

Notes'

Partial support 39

Notes'

Notes' Currently only supports MediaStream objects.

Safari

Full support 11

WebView Android Partial support 52

Notes'

Partial support 52

Notes'

Notes' Currently only supports MediaStream objects.

Chrome Android Partial support 52

Notes'

Partial support 52

Notes'

Notes' Currently only supports MediaStream objects.

Firefox Android Partial support 42

Notes'

Partial support 42

Notes'

Notes' Currently only supports MediaStream objects. No support 18 — 58

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

Opera Android Partial support 41

Notes'

Partial support 41

Notes'

Notes' Currently only supports MediaStream objects.

Safari iOS

Full support 11

Samsung Internet Android Partial support 6.0

Notes'

Partial support 6.0

Notes'

Notes' Currently only supports MediaStream objects.

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.'
See implementation notes.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.