W3cubDocs

/DOM

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 privious 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 compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Yes 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
MediaSource 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
Yes 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
sourceBuffers 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
activeSourceBuffers 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
readyState 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
duration 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
onsourceclose 53 17 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
onsourceended 53 17 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
onsourceopen 53 17 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
addSourceBuffer 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
removeSourceBuffer 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
endOfStream 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
setLiveSeekableRange 62 No No No 49 No
clearLiveSeekableRange 62 17 No No 49 No
isTypeSupported 31
31
23 — 31
Prefixed
Prefixed Requires the vendor prefix: -webkit-
12 42
42
25 — 42
Disabled
Limited support to a whitelist of sites, for example YouTube, Netflix, and other popular streaming sites. The whitelist 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.
11
11
Only works on Windows 8+.
15 8
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 4.4.3 33 Yes 41 30 No Yes
MediaSource 4.4.3 33 Yes 41 30 No Yes
sourceBuffers 4.4.3 33 Yes 41 30 No Yes
activeSourceBuffers 4.4.3 33 Yes 41 30 No Yes
readyState 4.4.3 33 Yes 41 30 No Yes
duration 4.4.3 33 Yes 41 30 No Yes
onsourceclose 4.4.3 33 Yes 41 30 No Yes
onsourceended 4.4.3 33 Yes 41 30 No Yes
onsourceopen 4.4.3 33 17 41 30 No Yes
addSourceBuffer 4.4.3 33 Yes 41 30 No Yes
removeSourceBuffer 4.4.3 33 Yes 41 30 No Yes
endOfStream 4.4.3 33 Yes 41 30 No Yes
setLiveSeekableRange 62 62 ? ? 49 ? No
clearLiveSeekableRange 62 62 ? ? 49 ? No
isTypeSupported 4.4.3 33 Yes 41 30 No Yes

See also

© 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/MediaSource