MediaSource: sourceopen event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Note: This feature is available in Dedicated Web Workers.

The sourceopen event is fired when a MediaSource object's readyState changes to "open". This indicates that the MediaSource is ready to receive data from SourceBuffer objects. This can occur either when the MediaSource object is first attached to a media element or when the readyState changes from "ended" back to "open".

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("sourceopen", (event) => {});

onsourceopen = (event) => {};

Event type

A generic Event.

Examples

Handling the sourceopen event

This example sets up a MediaSource, connects it to a video element, and listens for the sourceopen event. When the event fires, it adds a SourceBuffer to handle the video data, fetches the data, appends it to the buffer, and finally revokes the object URL when the source ends.

js
const video = document.getElementById("myVideo");
const mediaSource = new MediaSource();

video.src = URL.createObjectURL(mediaSource);

mediaSource.addEventListener("sourceopen", (event) => {
  console.log("MediaSource sourceopen:", event);
  // Add source buffers and begin adding media data.
  const sourceBuffer = mediaSource.addSourceBuffer(
    'video/mp4; codecs="avc1.42E01E"',
  );
  fetch("video-data.mp4")
    .then((response) => response.arrayBuffer())
    .then((data) => {
      sourceBuffer.appendBuffer(data);
    });
});

mediaSource.addEventListener("sourceended", () => {
  URL.revokeObjectURL(video.src);
});

Specifications

Specification
Media Source Extensions™
# dfn-sourceopen

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
sourceopen event
Chrome – Partial support
Chrome 31 – 52 (Release date: 2013-11-12)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Chrome – Full support
Chrome 53 (Release date: 2016-08-31)
footnote Full support
Edge – Partial support
Edge 12 – 16 (Release date: 2015-07-29)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Edge – Full support
Edge 17 (Release date: 2018-04-30)
footnote Full support
Firefox – Full support
Firefox 42 (Release date: 2015-11-03)
footnote Full support
Opera – Partial support
Opera 18 – 39 (Release date: 2013-11-19)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Opera – Full support
Opera 40 (Release date: 2016-09-20)
footnote Full support
Safari – Partial support
Safari 8 – 10 (Release date: 2014-10-16)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – Partial support
Chrome Android 31 – 52 (Release date: 2013-11-14)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Chrome Android – Full support
Chrome Android 53 (Release date: 2016-09-07)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Partial support
Opera Android 18 – 37 (Release date: 2013-11-20)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – Partial support
Safari on iOS 13 (Release date: 2019-09-19)
footnote Partial support
footnote Exposed in Mobile Safari on iPad but not on iPhone.
Samsung Internet – Partial support
Samsung Internet 2 – 5.4 (Release date: 2014-10-17)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Partial support
WebView Android 4.4.3 – 52 (Release date: 2014-06-02)
footnote Partial support
footnote The onsourceopen event handler property is not supported.
WebView Android – Full support
WebView Android 53 (Release date: 2016-09-07)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Has more compatibility info.