HTMLMediaElement.srcObject

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 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 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 media source 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

This version of the example above supports older browser versions that require you to create an object URL and assign it to src if srcObject isn't supported.

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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Partial support 52
Notes
Partial support 52
Notes
Notes Currently only supports MediaStream objects.
Edge Partial support Partial
Notes
Partial support Partial
Notes
Notes Currently only supports MediaStream objects.
Firefox Partial support Partial
Notes
Partial support Partial
Notes
Notes Currently only supports MediaStream objects.
No support 18 — 58
Prefixed
Prefixed Implemented with the vendor prefix: moz
IE ? Opera Partial support 39
Notes
Partial support 39
Notes
Notes Currently only supports MediaStream objects.
Safari ? 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.
Edge Mobile Partial support Partial
Notes
Partial support Partial
Notes
Notes Currently only supports MediaStream objects.
Firefox Android Partial support Partial
Notes
Partial support Partial
Notes
Notes Currently only supports MediaStream objects.
No support 18 — 58
Prefixed
Prefixed Implemented with the vendor prefix: moz
Opera Android Partial support 39
Notes
Partial support 39
Notes
Notes Currently only supports MediaStream objects.
Safari iOS ? Samsung Internet Android ?

Legend

Partial support  
Partial support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

Document Tags and Contributors

Last updated by: Sheppy,