MediaSource: sourceended 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 sourceended event is fired when a MediaSource object's readyState changes to "ended". This indicates that the application has finished sending data to the MediaSource. When an application has finished appending all media data to the SourceBuffer objects associated with a MediaSource, it calls the MediaSource.endOfStream() method on the MediaSource. This causes the readyState to transition to "ended" and triggers the sourceended event.

Syntax

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

js
addEventListener("sourceended", (event) => {})

onsourceended = (event) => {}

Event type

A generic Event.

Examples

Handling the sourceopen event

This example demonstrates setting up a video element for playback and handling the sourceended event for proper resource management. The code sets up a MediaSource, initiates playback by fetching and buffering video data, and then uses the sourceended event to perform cleanup tasks like removing event listeners and notifying the user when playback is complete.

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

video.src = URL.createObjectURL(mediaSource);

mediaSource.addEventListener("sourceopen", (event) => {
  const sourceBuffer = mediaSource.addSourceBuffer(
    'video/mp4; codecs="avc1.42E01E"',
  );

  fetch("video-data.mp4")
    .then((response) => response.arrayBuffer())
    .then((data) => {
      sourceBuffer.appendBuffer(data);
      sourceBuffer.addEventListener("updateend", () => {
        mediaSource.endOfStream();
      });
    });
});

mediaSource.addEventListener("sourceended", (event) => {
  console.log("MediaSource sourceended:", event);
  URL.revokeObjectURL(video.src);
  // Perform cleanup

  // Remove event listeners from SourceBuffer and MediaSource
  sourceBuffer.removeEventListener("updateend", () => {});
  mediaSource.removeEventListener("sourceopen", () => {});

  // Notify user (e.g., display a "Playback finished" message)
  const messageElement = document.createElement("p");
  messageElement.textContent = "Playback finished.";
  document.body.appendChild(messageElement);
});

Specifications

Specification
Media Source Extensions™
# dfn-sourceended

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
sourceended event
Chrome – Partial support
Chrome 31 – 52 (Release date: 2013-11-12)
footnote Partial support
footnote The onsourceended 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 onsourceended 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 onsourceended 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 onsourceended 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 onsourceended 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 onsourceended 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 onsourceended 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 onsourceclose 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.

See also

MediaSource.endOfStream()