MediaSource: handle property
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Note: This feature is only available in Dedicated Web Workers.
Thuộc tính chỉ đọc handle của giao diện MediaSource trả về một đối tượng MediaSourceHandle, là proxy cho MediaSource có thể được chuyển từ một dedicated worker trở lại luồng chính và gắn vào media element thông qua thuộc tính HTMLMediaElement.srcObject.
Note:
handle chỉ hiển thị trên các thực thể MediaSource bên trong dedicated workers.
Mỗi đối tượng MediaSource được tạo bên trong một dedicated worker đều có đối tượng MediaSourceHandle riêng biệt của nó. Getter handle sẽ luôn trả về thực thể MediaSourceHandle cụ thể cho thực thể MediaSource của dedicated worker liên quan. Nếu handle đã được chuyển sang luồng chính bằng postMessage(), thì thực thể handle trong worker về mặt kỹ thuật đã bị tách rời và không thể được chuyển lại nữa.
Giá trị
Một thực thể đối tượng MediaSourceHandle.
Ví dụ
Thuộc tính handle có thể được truy cập bên trong một dedicated worker và đối tượng MediaSourceHandle thu được sau đó được chuyển sang luồng đã tạo worker đó (trong trường hợp này là luồng chính) thông qua lời gọi postMessage():
// Inside dedicated worker
let mediaSource = new MediaSource();
let handle = mediaSource.handle;
// Transfer the handle to the context that created the worker
postMessage({ arg: handle }, [handle]);
mediaSource.addEventListener("sourceopen", () => {
// Await sourceopen on MediaSource before creating SourceBuffers
// and populating them with fetched media — MediaSource won't
// accept creation of SourceBuffers until it is attached to the
// HTMLMediaElement and its readyState is "open"
});
Ở phía luồng chính, ta nhận handle qua trình xử lý sự kiện message, gắn nó vào một <video> thông qua thuộc tính HTMLMediaElement.srcObject, và phát video bằng play:
worker.addEventListener("message", (msg) => {
let mediaSourceHandle = msg.data.arg;
video.srcObject = mediaSourceHandle;
video.play();
});
Note:
MediaSourceHandle không thể được chuyển thành công vào hoặc qua shared worker hoặc service worker.
Thông số kỹ thuật
| Specification |
|---|
| Media Source Extensions™> # dom-mediasource-handle> |