大网数据广告活动
Web 标准与前端开发

ManagedMediaSource

· 52CE 我爱测

Media Source Extensionswindow_and_dedicated

Media Source Extensionswindow_and_dedicated

The ManagedMediaSource interface of the Media Source Extensions API is a MediaSource that actively manages its memory content. Unlike a regular MediaSource, the user agent can evict content from its ManagedSourceBuffer objects at any time, for reasons such as memory or hardware limitations. This makes it suitable for power-efficient streaming scenarios where the user agent needs more control over buffered media data.

When MediaSource.addSourceBuffer is called on a ManagedMediaSource, it creates ManagedSourceBuffer objects (instead of SourceBuffer objects), which fire ManagedSourceBuffer.bufferedchange_event events to notify the application when buffered ranges are modified by the user agent.

[!NOTE] On Safari, ManagedMediaSource only activates when remote playback is explicitly disabled on the media element (by setting HTMLMediaElement.disableRemotePlayback to true), or when an AirPlay source alternative is provided (for example, an HLS source element). Without either of these, the MediaSource.sourceopen_event event will not fire.

Constructor

  • ManagedMediaSource.ManagedMediaSource
    • : Creates and returns a new ManagedMediaSource object instance with no associated source buffers.

Instance properties

Also inherits properties from its parent interface, MediaSource.

  • ManagedMediaSource.streaming
    • : A boolean indicating whether the ManagedMediaSource object is currently streaming. When true, the application should actively fetch and append media data. When false, the application can stop fetching new data.

Instance methods

Inherits methods from its parent interface, MediaSource.

Events

Also inherits events from its parent interface, MediaSource.

  • ManagedMediaSource.startstreaming_event
    • : Fired when the ManagedMediaSource's ManagedMediaSource.streaming property changes from false to true, meaning the media source has started streaming.
  • ManagedMediaSource.endstreaming_event
    • : Fired when the ManagedMediaSource's ManagedMediaSource.streaming property changes from true to false, meaning the media source has stopped streaming.

Examples

Setting up a managed media source

The following example sets up a ManagedMediaSource, connects it to a video element, and listens for the ManagedMediaSource.startstreaming_event and ManagedMediaSource.endstreaming_event events to control when media data is fetched. ManagedSourceBuffer.bufferedchange_event events are logged below the video.

const videoUrl =
  "https://mdn.github.io/shared-assets/videos/flower-fragmented.mp4";
const mediaType = 'video/mp4; codecs="avc1.64001F, mp4a.40.2"';
const video = document.querySelector("video");

if (!window.ManagedMediaSource?.isTypeSupported(mediaType)) {
  console.log("ManagedMediaSource is not supported in this browser.");
} else {
  const source = new ManagedMediaSource();
  video.disableRemotePlayback = true;
  video.src = URL.createObjectURL(source);

  source.addEventListener("sourceopen", () => {
    const sourceBuffer = source.addSourceBuffer(mediaType);

    sourceBuffer.addEventListener("bufferedchange", (event) => {
      for (let i = 0; i < event.addedRanges.length; i++) {
        console.log(
          `Buffered: ${event.addedRanges.start(i).toFixed(2)}s – ${event.addedRanges.end(i).toFixed(2)}s`,
        );
      }
    });

    source.addEventListener("startstreaming", async () => {
      console.log("startstreaming — fetching media data…");
      const response = await fetch(videoUrl);
      const data = await response.arrayBuffer();
      sourceBuffer.appendBuffer(data);
    });

    source.addEventListener("endstreaming", () => {
      console.log("endstreaming — enough data buffered");
    });
  });
}

dom-examples/media-source-extensions/managed-media-source/

Specifications

Browser compatibility

See also

  • MediaSource
  • ManagedSourceBuffer
  • BufferedChangeEvent
  • SourceBuffer

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。