BrowserCaptureMediaStreamTrack: phương thức restrictTo()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Phương thức restrictTo() của giao diện BrowserCaptureMediaStreamTrack giới hạn một luồng tự quay vào một phần tử DOM cụ thể (và các phần tử con của nó).
Syntax
restrictTo(restrictionTarget)
Parameters
restrictionTarget-
Một thực thể
RestrictionTargetbiểu diễn phần tử mà luồng cần được giới hạn vào, hoặcnull/undefined, trong trường hợp đó mọi giới hạn đã đặt trước đó sẽ được gỡ khỏi track.
Return value
Một Promise hoàn tất với undefined.
Promise sẽ bị từ chối nếu:
- Track
kindkhông phải là"video", hoặcreadyStatecủa nó không phải là"live". - Phần tử restriction target không còn tồn tại.
- Track đang bị giới hạn không phải là track được ghi lại từ màn hình của người dùng.
restrictionTargetkhông phải là một thực thểRestrictionTarget,null, hoặcundefined.restrictionTargetđược tạo trong một tab khác với tab đang được ghi lại.
Note:
Trong Chromium, nếu một track có các bản sao, restrictTo() sẽ bị từ chối (xem Chrome issue 41482026).
Examples
>Ví dụ giới hạn cơ bản
// Options for getDisplayMedia()
const displayMediaOptions = {
preferCurrentTab: true,
};
// Create restriction target from DOM element
const demoElem = document.querySelector("#demo");
const restrictionTarget = await RestrictionTarget.fromElement(demoElem);
// Capture video stream from user's webcam and isolate video track
const stream =
await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
const [track] = stream.getVideoTracks();
// Restrict video track
await track.restrictTo(restrictionTarget);
// Broadcast restricted stream in <video> element
videoElem.srcObject = stream;
Xem Using the Element Capture and Region Capture APIs để biết mã ví dụ trong ngữ cảnh thực tế.
Dừng giới hạn
Bạn có thể dừng việc giới hạn bằng cách gọi restrictTo() trên chính track đó và truyền đối số null:
// Stop restricting
await track.restrictTo(null);
Specifications
| Specification |
|---|
| Element Capture> # dom-browsercapturemediastreamtrack-restrictto> |