SourceBuffer: appendBuffer() method

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 appendBuffer() method of the SourceBuffer interface appends media segment data from an ArrayBuffer, a TypedArray or a DataView object to the SourceBuffer.

Syntax

js
appendBuffer(source)

Parameters

source

Either an ArrayBuffer, a TypedArray or a DataView object that contains the media segment data you want to add to the SourceBuffer.

Return value

None (undefined).

Exceptions

InvalidStateError DOMException

Thrown in one of the following cases:

  • The SourceBuffer object's updating attribute is true. You must wait for any previous append, update, or remove operations to complete (indicated by the updateend event) before calling appendBuffer() again.
  • The SourceBuffer has been removed from the sourceBuffers attribute of the parent media source.
  • The HTMLMediaElement's error attribute is not null.
QuotaExceededError

The buffer is full, and no more data can be appended. This might occur if the SourceBuffer has reached a browser-defined limit on the amount of buffered data.

Additionally, errors can occur after the updatestart event has been fired and the appendBuffer() method has returned: for example, because the buffer contained bytes that were incorrectly formatted. In this situation the error event will be fired on this SourceBuffer instance.

Examples

Basic usage

This example demonstrates adding video data to a video element for playback. The MediaSource provides the video data, and the SourceBuffer adds that data. The example fetches video segment data, appends it to the SourceBuffer, and ends the stream when finished.

js
const mediaSource = new MediaSource();
const video = document.querySelector("video");
video.src = URL.createObjectURL(mediaSource);

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

  const buffer = await fetch("/my-video-segment.mp4").then((res) =>
    res.arrayBuffer(),
  );
  sourceBuffer.appendBuffer(buffer);
  sourceBuffer.addEventListener("updateend", () => {
    if (mediaSource.readyState === "open") {
      mediaSource.endOfStream();
    }
  });
});

Handling errors

This example demonstrates how to handle errors that may occur when calling appendBuffer().

It calls appendBuffer() inside a try...catch block to catch and handle the exceptions that the method synchronously throws. It also listens for the error event to handle errors that occur after appendBuffer() has returned, while the buffer is being asynchronously updated.

js
sourceBuffer.addEventListener("error", (e) => {
  console.error("Error appending buffer:", e);
  // Handle the error appropriately, e.g., show a message to the user,
  // try a different source, or stop playback.
});

try {
  sourceBuffer.appendBuffer(data);
} catch (e) {
  if (e.name === "InvalidStateError") {
    console.error(
      "InvalidStateError: The SourceBuffer is in an invalid state.",
    );
  } else if (e.name === "QuotaExceededError") {
    console.error("QuotaExceededError: The buffer is full.");
  } else {
    console.error("An unexpected error occurred:", e);
  }
}

Specifications

Specification
Media Source Extensions™
# dom-sourcebuffer-appendbuffer

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
appendBuffer
Chrome – Full support
Chrome 23 (Release date: 2012-11-06)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 42 (Release date: 2015-11-03)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote Full support
Chrome Android – Full support
Chrome Android 33 (Release date: 2014-02-26)
footnote Full support
Firefox for Android – Full support
Firefox for Android 42 (Release date: 2015-11-03)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
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 – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 4.4.3 (Release date: 2014-06-02)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Available in workers
Experimental
Chrome – Full support
Chrome 108 (Release date: 2022-11-29)
footnote Full support
Edge – Full support
Edge 108 (Release date: 2022-12-05)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 94 (Release date: 2022-12-15)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 108 (Release date: 2022-11-29)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 73 (Release date: 2023-01-17)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 108 (Release date: 2022-11-29)
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
Experimental. Expect behavior to change in the future.

See also