MediaSource

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

The MediaSource interface of the Media Source Extensions API represents a source of media data for an HTMLMediaElement object. A MediaSource object can be attached to a HTMLMediaElement to be played in the user agent.

Constructor

MediaSource()
Constructs and returns a new MediaSource object with no associated source buffers.

Properties

MediaSource.sourceBuffers Read only
Returns a SourceBufferList object containing the list of SourceBuffer objects associated with this MediaSource.
MediaSource.activeSourceBuffers Read only
Returns a SourceBufferList object containing a subset of the SourceBuffer objects contained within MediaSource.sourceBuffers — the list of objects providing the selected video track, enabled audio tracks, and shown/hidden text tracks.
MediaSource.readyState Read only
Returns an enum representing the state of the current MediaSource, whether it is not currently attached to a media element (closed), attached and ready to receive SourceBuffer objects (open), or attached but the stream has been ended via MediaSource.endOfStream() (ended.)
MediaSource.duration
Gets and sets the duration of the current media being presented.

Event handlers

MediaSource.onsourceclose
The event handler for the sourceclose event.
MediaSource.onsourceended
The event handler for the sourceended event.
MediaSource.onsourceopen
The event handler for the sourceopen event.

Methods

Inherits methods from its parent interface, EventTarget.

MediaSource.addSourceBuffer()
Creates a new SourceBuffer of the given MIME type and adds it to the MediaSource.sourceBuffers list.
MediaSource.clearLiveSeekableRange()
Clears a seekable range previously set with a call to setLiveSeekableRange().
MediaSource.endOfStream()
Signals the end of the stream.
MediaSource.removeSourceBuffer()
Removes the given SourceBuffer from the MediaSource.sourceBuffers list.
MediaSource.setLiveSeekableRange()
Sets the range that the user can seek to in the media element.

Static methods

MediaSource.isTypeSupported()
Returns a Boolean value indicating if the given MIME type is supported by the current user agent — this is, if it can successfully create SourceBuffer objects for that MIME type.

Examples

The following simple example loads a video with XMLHttpRequest, playing it as soon as it can. This example was written by Nick Desaulniers and can be viewed live here (you can also download the source for further investigation.)

var video = document.querySelector('video');

var assetURL = 'frag_bunny.mp4';
// Need to be specific for Blink regarding codecs
// ./mp4info frag_bunny.mp4 | grep Codec
var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';

if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) {
  var mediaSource = new MediaSource();
  //console.log(mediaSource.readyState); // closed
  video.src = URL.createObjectURL(mediaSource);
  mediaSource.addEventListener('sourceopen', sourceOpen);
} else {
  console.error('Unsupported MIME type or codec: ', mimeCodec);
}

function sourceOpen (_) {
  //console.log(this.readyState); // open
  var mediaSource = this;
  var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
  fetchAB(assetURL, function (buf) {
    sourceBuffer.addEventListener('updateend', function (_) {
      mediaSource.endOfStream();
      video.play();
      //console.log(mediaSource.readyState); // ended
    });
    sourceBuffer.appendBuffer(buf);
  });
};

function fetchAB (url, cb) {
  console.log(url);
  var xhr = new XMLHttpRequest;
  xhr.open('get', url);
  xhr.responseType = 'arraybuffer';
  xhr.onload = function () {
    cb(xhr.response);
  };
  xhr.send();
};

Specifications

Specification Status Comment
Media Source Extensions
The definition of 'MediaSource' in that specification.
Recommendation Initial definition.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
MediaSource
Experimental
Chrome Full support 31
Full support 31
No support 23 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11Opera Full support 18
Full support 18
No support 15 — 18
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 31
Full support 31
No support 25 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Firefox Android Full support 41Opera Android Full support 18
Full support 18
No support 14 — 18
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Safari iOS Full support 8Samsung Internet Android Full support 2.0
Full support 2.0
No support 1.5 — 2.0
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
MediaSource() constructor
Experimental
Chrome Full support 31
Full support 31
No support 23 — 31
Prefixed
Prefixed Implemented with the vendor prefix: WebKit
Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
activeSourceBuffers
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
addSourceBuffer
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
clearLiveSeekableRange
Experimental
Chrome Full support 62Edge Full support 17Firefox No support NoIE No support NoOpera Full support 49Safari No support NoWebView Android Full support 62Chrome Android Full support 62Firefox Android ? Opera Android Full support 46Safari iOS No support NoSamsung Internet Android Full support 8.0
duration
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
endOfStream
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
isTypeSupported
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
onsourceclose
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
onsourceended
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
onsourceopen
Experimental
Chrome Full support 53Edge Full support 17Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
readyState
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 33Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 2.0
removeSourceBuffer
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5
setLiveSeekableRange
Experimental
Chrome Full support 62Edge Full support 17Firefox No support NoIE No support NoOpera Full support 49Safari No support NoWebView Android Full support 62Chrome Android Full support 62Firefox Android ? Opera Android Full support 46Safari iOS No support NoSamsung Internet Android Full support 8.0
sourceBuffers
Experimental
Chrome Full support 23Edge Full support 12Firefox Full support 42
Full support 42
No support 25 — 42
Notes Disabled
Notes Limited support to an allowed list of sites, for example YouTube, Netflix, and other popular streaming sites. The limitation was removed when Media Source Extensions was enabled by default in Firefox 42.
Disabled From version 25 until version 42 (exclusive): this feature is behind the media.mediasource.enabled preference. To change preferences in Firefox, visit about:config.
IE Full support 11
Notes
Full support 11
Notes
Notes Only works on Windows 8+.
Opera Full support 15Safari Full support 8WebView Android Full support 4.4.3Chrome Android Full support 25Firefox Android Full support 41Opera Android Full support 14Safari iOS No support NoSamsung Internet Android Full support 1.5

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

See also