MediaSource
Experimental: 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 ofSourceBuffer
objects associated with thisMediaSource
. MediaSource.activeSourceBuffers
Read only-
Returns a
SourceBufferList
object containing a subset of theSourceBuffer
objects contained withinMediaSource.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 receiveSourceBuffer
objects (open
), or attached but the stream has been ended viaMediaSource.endOfStream()
(ended
.) MediaSource.duration
-
Gets and sets the duration of the current media being presented.
Methods
Inherits methods from its parent interface, EventTarget
.
MediaSource.addSourceBuffer()
-
Creates a new
SourceBuffer
of the given MIME type and adds it to theMediaSource.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 theMediaSource.sourceBuffers
list. MediaSource.setLiveSeekableRange()
-
Sets the range that the user can seek to in the media element.
Events
sourceclose
-
Fired when the
MediaSource
instance is not attached to a media element anymore. sourceended
-
Fired when the
MediaSource
instance is still attached to a media element, butendOfStream()
has been called. sourceopen
-
Fired when the
MediaSource
instance has been opened by a media element and is ready for data to be appended to theSourceBuffer
objects insourceBuffers
.
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 |
---|
Media Source Extensions™ # mediasource |
Browser compatibility
BCD tables only load in the browser