ManagedMediaSource: sự kiện endstreaming

Khả dụng hạn chế

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.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Sự kiện endstreaming của giao diện ManagedMediaSource được kích hoạt khi thuộc tính streaming thay đổi từ true sang false. Điều này cho thấy tác nhân người dùng đã có đủ dữ liệu được đệm để đảm bảo phát lại không bị gián đoạn, và ứng dụng có thể dừng tải các phân đoạn media mới.

Cú pháp

Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính trình xử lý sự kiện.

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

onendstreaming = (event) => {};

Kiểu sự kiện

Một Event chung.

Ví dụ

Tạm dừng tải dữ liệu khi nhận sự kiện endstreaming

Ví dụ này tạo một ManagedMediaSource, gắn nó vào phần tử <video>, và sử dụng các sự kiện startstreamingendstreaming để kiểm soát thời điểm tải các phân đoạn media.

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

if (ManagedMediaSource.isTypeSupported(mediaType)) {
  const video = document.createElement("video");
  const source = new ManagedMediaSource();

  video.controls = true;
  video.disableRemotePlayback = true;
  video.src = URL.createObjectURL(source);
  document.body.appendChild(video);

  let shouldFetch = false;

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

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

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

Thông số kỹ thuật

Thông số kỹ thuật
Media Source Extensions™
# dfn-endstreaming

Tương thích trình duyệt

Xem thêm