Hướng dẫn javascript record screen and audio - javascript ghi lại màn hình và âm thanh

Trong bài viết này, chúng tôi sẽ kiểm tra cách sử dụng API chụp màn hình và phương thức

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 của nó để chụp một phần hoặc tất cả màn hình để phát trực tuyến, ghi hoặc chia sẻ trong phiên hội nghị WEBRTC.

Lưu ý: Có thể hữu ích khi lưu ý rằng các phiên bản gần đây của bộ điều hợp WEBRTC.js Shim bao gồm các triển khai

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 để cho phép chia sẻ màn hình trên các trình duyệt hỗ trợ nhưng không triển khai API tiêu chuẩn hiện tại. Điều này hoạt động với ít nhất Chrome, Edge và Firefox. It may be useful to note that recent versions of the WebRTC adapter.js shim include implementations of
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 to enable screen sharing on browsers that support it but do not implement the current standard API. This works with at least Chrome, Edge, and Firefox.

Nắm bắt nội dung màn hình

Nắm bắt nội dung màn hình dưới dạng trực tiếp

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 được bắt đầu bằng cách gọi
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
6, trả về một lời hứa giải quyết cho một luồng chứa nội dung màn hình trực tiếp.

Bắt đầu chụp màn hình: ________ 17/________ 18 Phong cách

async function startCapture(displayMediaOptions) {
  let captureStream = null;

  try {
    captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
  } catch (err) {
    console.error(`Error: ${err}`);
  }
  return captureStream;
}

Bạn có thể viết mã này bằng cách sử dụng hàm không đồng bộ và toán tử

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
8, như được hiển thị ở trên hoặc sử dụng trực tiếp
const gdmOptions = {
  video: true,
  audio: true
}
0, như được thấy dưới đây.

Bắt đầu chụp màn hình: const gdmOptions = { video: true, audio: true } 0 Phong cách

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}

Dù bằng cách nào, tác nhân người dùng phản hồi bằng cách trình bày giao diện người dùng nhắc người dùng chọn vùng màn hình để chia sẻ. Cả hai triển khai này của

const gdmOptions = {
  video: true,
  audio: true
}
2 trả về
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 chứa hình ảnh hiển thị được chụp.

Xem các tùy chọn và ràng buộc, bên dưới, để biết thêm về cả cách chỉ định loại bề mặt bạn muốn cũng như các cách khác để điều chỉnh luồng kết quả.

Ví dụ về cửa sổ cho phép người dùng chọn bề mặt hiển thị để chụp

Hướng dẫn javascript record screen and audio - javascript ghi lại màn hình và âm thanh

Sau đó, bạn có thể sử dụng luồng bị bắt,

const gdmOptions = {
  video: true,
  audio: true
}
4, cho bất cứ điều gì chấp nhận luồng làm đầu vào. Các ví dụ dưới đây cho thấy một vài cách để sử dụng luồng.

Có thể nhìn thấy so với bề mặt hiển thị logic

Đối với các mục đích của API chụp màn hình, bề mặt hiển thị là bất kỳ đối tượng nội dung nào có thể được API chọn cho mục đích chia sẻ. Chia sẻ bề mặt bao gồm nội dung của tab Trình duyệt, cửa sổ hoàn chỉnh, tất cả các cửa sổ của một ứng dụng được kết hợp thành một bề mặt và một màn hình (hoặc nhóm màn hình kết hợp với nhau thành một bề mặt).display surface is any content object that can be selected by the API for sharing purposes. Sharing surfaces include the contents of a browser tab, a complete window, all of the windows of an application combined into a single surface, and a monitor (or group of monitors combined together into one surface).

Có hai loại bề mặt hiển thị. Bề mặt hiển thị có thể nhìn thấy là một bề mặt hoàn toàn có thể nhìn thấy trên màn hình, chẳng hạn như cửa sổ phía trước hoặc tab hoặc toàn bộ màn hình.visible display surface is a surface which is entirely visible on the screen, such as the frontmost window or tab, or the entire screen.

Một bề mặt hiển thị logic là một bề mặt một phần hoặc bị che khuất hoàn toàn, bằng cách bị chồng chéo bởi một đối tượng khác ở một mức độ nào đó, hoặc bằng cách bị ẩn hoàn toàn hoặc ngoài màn hình. Cách chúng được xử lý bởi API chụp màn hình khác nhau. Nói chung, trình duyệt sẽ cung cấp một hình ảnh che khuất phần ẩn của bề mặt hiển thị logic theo một cách nào đó, chẳng hạn như bằng cách làm mờ hoặc thay thế bằng một màu hoặc hoa văn. Điều này được thực hiện vì lý do bảo mật, vì nội dung mà người dùng không thể nhìn thấy có thể chứa dữ liệu mà họ không muốn chia sẻ.logical display surface is one which is in part or completely obscured, either by being overlapped by another object to some extent, or by being entirely hidden or offscreen. How these are handled by the Screen Capture API varies. Generally, the browser will provide an image which obscures the hidden portion of the logical display surface in some way, such as by blurring or replacing with a color or pattern. This is done for security reasons, as the content that cannot be seen by the user may contain data which they do not want to share.

Một tác nhân người dùng có thể cho phép chụp toàn bộ nội dung của một cửa sổ bị che khuất sau khi nhận được sự cho phép của người dùng để làm như vậy. Trong trường hợp này, tác nhân người dùng có thể bao gồm nội dung bị che khuất, bằng cách lấy nội dung hiện tại của phần ẩn của cửa sổ hoặc bằng cách trình bày các nội dung có thể xem xét rõ ràng nhất nếu không có nội dung hiện tại.

Tùy chọn và ràng buộc

Đối tượng ràng buộc được truyền vào

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 là một đối tượng được sử dụng để cấu hình luồng kết quả.

Lưu ý: Không giống như hầu hết các cách sử dụng các ràng buộc trong API phương tiện, ở đây nó chỉ được sử dụng để xác định cấu hình luồng và không lọc các lựa chọn có sẵn. Unlike most uses of constraints in media APIs, here it's solely used to define the stream configuration, and not to filter the available choices.

Xem các thuộc tính của các bản nhạc được chia sẻ để biết chi tiết về các ràng buộc bổ sung để định cấu hình luồng ghi màn hình được thêm vào

const gdmOptions = {
  video: true,
  audio: true
}
6,
const gdmOptions = {
  video: true,
  audio: true
}
7 và
const gdmOptions = {
  video: true,
  audio: true
}
8).

Không có ràng buộc nào được áp dụng theo bất kỳ cách nào cho đến khi sau khi nội dung nắm bắt đã được chọn. Các ràng buộc thay đổi những gì bạn thấy trong luồng kết quả.

Ví dụ: nếu bạn chỉ định ràng buộc

const gdmOptions = {
  video: true,
  audio: true
}
9 cho video, nó được áp dụng bằng cách chia tỷ lệ video sau khi người dùng chọn khu vực để chia sẻ. Nó không thiết lập một hạn chế về kích thước của chính nguồn.

Lưu ý: Các ràng buộc không bao giờ gây ra các thay đổi trong danh sách các nguồn có sẵn để chụp bởi API chia sẻ màn hình. Điều này đảm bảo rằng các ứng dụng web không thể buộc người dùng chia sẻ nội dung cụ thể bằng cách hạn chế danh sách nguồn cho đến khi chỉ còn một mục. Constraints never cause changes to the list of sources available for capture by the Screen Sharing API. This ensures that web applications can't force the user to share specific content by restricting the source list until only one item is left.

Mặc dù chụp hiển thị có hiệu lực, máy chia sẻ nội dung màn hình sẽ hiển thị một số dạng chỉ báo để người dùng biết rằng việc chia sẻ đang diễn ra.

Lưu ý: Vì lý do bảo mật và bảo mật, các nguồn chia sẻ màn hình không phải là người sử dụng

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
0. Liên quan đến điều này, sự kiện
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
1 không bao giờ được gửi khi có những thay đổi đối với các nguồn có sẵn cho
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3.
For privacy and security reasons, screen sharing sources are not enumerable using
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
0. Related to this, the
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
1 event is never sent when there are changes to the sources available for
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3.

Chụp âm thanh chia sẻ

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 được sử dụng phổ biến nhất để quay video màn hình của người dùng (hoặc các bộ phận của chúng). Tuy nhiên, các tác nhân người dùng có thể cho phép chụp âm thanh cùng với nội dung video. Nguồn của âm thanh này có thể là cửa sổ đã chọn, toàn bộ hệ thống âm thanh của máy tính hoặc micrô của người dùng (hoặc kết hợp tất cả các bên trên).

Trước khi bắt đầu một dự án yêu cầu chia sẻ âm thanh, hãy chắc chắn kiểm tra khả năng tương thích của trình duyệt trong

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
4 để xem liệu trình duyệt bạn có muốn tương thích với hỗ trợ âm thanh trong các luồng màn hình bị bắt không.

Để yêu cầu màn hình được chia sẻ với âm thanh bao gồm, các tùy chọn được chuyển vào

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 có thể trông như thế này:

const gdmOptions = {
  video: true,
  audio: true
}

Điều này cho phép người dùng hoàn toàn tự do chọn bất cứ điều gì họ muốn, trong giới hạn của những gì tác nhân người dùng hỗ trợ. Điều này có thể được tinh chỉnh thêm bằng cách chỉ định thông tin bổ sung cho mỗi

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
6 và
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
7:

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}

Trong ví dụ này, con trỏ sẽ luôn có thể nhìn thấy trong việc chụp và bản nhạc âm thanh lý tưởng sẽ có các tính năng hủy tiếng ồn và hủy tiếng vang, cũng như tốc độ mẫu âm thanh lý tưởng là 44,1kHz.

Việc chụp âm thanh luôn là tùy chọn và ngay cả khi nội dung web yêu cầu một luồng với cả âm thanh và video,

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 được trả lại vẫn có thể chỉ có một bản nhạc video, không có âm thanh.

Lưu ý: Một số thuộc tính không được thực hiện rộng rãi và có thể không được sử dụng bởi động cơ.

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
9, ví dụ, có hỗ trợ hạn chế. Some properties are not widely implemented and might not be used by the engine.
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
9, for example, has limited support.

Sử dụng luồng bị bắt

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
0 được trả về bởi
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 giải quyết thành một
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 chứa ít nhất một luồng video chứa màn hình hoặc khu vực màn hình và được điều chỉnh hoặc lọc dựa trên các ràng buộc được chỉ định khi
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 được gọi.

Rủi ro tiềm ẩn

Các vấn đề về quyền riêng tư và bảo mật xung quanh việc chia sẻ màn hình thường không quá nghiêm trọng, nhưng chúng tồn tại. Vấn đề tiềm năng lớn nhất là người dùng vô tình chia sẻ nội dung mà họ không muốn chia sẻ.

Ví dụ: vi phạm quyền riêng tư và/hoặc bảo mật có thể dễ dàng xảy ra nếu người dùng chia sẻ màn hình của họ và cửa sổ nền có thể nhìn thấy có chứa thông tin cá nhân hoặc nếu trình quản lý mật khẩu của họ có thể nhìn thấy trong luồng chung. Hiệu ứng này có thể được khuếch đại khi chụp các bề mặt hiển thị logic, có thể chứa nội dung mà người dùng không biết gì cả, chứ đừng nói đến việc xem.

Các tác nhân người dùng coi trọng quyền riêng tư nên làm xáo trộn nội dung không thực sự hiển thị trên màn hình, trừ khi được ủy quyền được trao để chia sẻ nội dung đó một cách cụ thể.

Trước khi phát trực tuyến nội dung màn hình đã chụp có thể bắt đầu, tác nhân người dùng sẽ yêu cầu người dùng xác nhận yêu cầu chia sẻ và để chọn nội dung để chia sẻ.

Ví dụ

Chụp màn hình đơn giản

Trong ví dụ này, nội dung của khu vực màn hình đã chụp được truyền thành một phần tử

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
4 trên cùng một trang.

JavaScript

Không có nhiều mã cần thiết để thực hiện công việc này và nếu bạn quen thuộc với việc sử dụng

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
5 để quay video từ máy ảnh, bạn sẽ thấy
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 rất quen thuộc.

Thành lập

Đầu tiên, một số hằng số được thiết lập để tham chiếu các phần tử trên trang mà chúng ta sẽ cần truy cập:

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
4 trong đó các nội dung màn hình đã chụp sẽ được truyền phát, một hộp mà đầu ra đã đăng nhập sẽ được rút ra và các nút bắt đầu và dừng dừng lại Điều đó sẽ bật và tắt chụp hình ảnh màn hình.

Đối tượng

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
8 chứa các ràng buộc để truyền vào
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3; Ở đây, thuộc tính
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
9 được đặt thành
console.log = (msg) => logElem.innerHTML += `${msg}<br>`;
console.error = (msg) => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
console.warn = (msg) => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
console.info = (msg) => logElem.innerHTML += `<span class="info">${msg}</span><br>`;
1, chỉ ra rằng con trỏ chuột phải luôn được đưa vào các phương tiện bị bắt.

Lưu ý: Một số thuộc tính không được thực hiện rộng rãi và có thể không được sử dụng bởi động cơ.

const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
9, ví dụ, có hỗ trợ hạn chế. Some properties are not widely implemented and might not be used by the engine.
const gdmOptions = {
  video: {
    cursor: "always"
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100
  }
}
9, for example, has limited support.

Sử dụng luồng bị bắt

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
0 được trả về bởi
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 giải quyết thành một
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 chứa ít nhất một luồng video chứa màn hình hoặc khu vực màn hình và được điều chỉnh hoặc lọc dựa trên các ràng buộc được chỉ định khi
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
3 được gọi.

Rủi ro tiềm ẩn

console.log = (msg) => logElem.innerHTML += `${msg}<br>`;
console.error = (msg) => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
console.warn = (msg) => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
console.info = (msg) => logElem.innerHTML += `<span class="info">${msg}</span><br>`;

Các vấn đề về quyền riêng tư và bảo mật xung quanh việc chia sẻ màn hình thường không quá nghiêm trọng, nhưng chúng tồn tại. Vấn đề tiềm năng lớn nhất là người dùng vô tình chia sẻ nội dung mà họ không muốn chia sẻ.

Ví dụ: vi phạm quyền riêng tư và/hoặc bảo mật có thể dễ dàng xảy ra nếu người dùng chia sẻ màn hình của họ và cửa sổ nền có thể nhìn thấy có chứa thông tin cá nhân hoặc nếu trình quản lý mật khẩu của họ có thể nhìn thấy trong luồng chung. Hiệu ứng này có thể được khuếch đại khi chụp các bề mặt hiển thị logic, có thể chứa nội dung mà người dùng không biết gì cả, chứ đừng nói đến việc xem.

Các tác nhân người dùng coi trọng quyền riêng tư nên làm xáo trộn nội dung không thực sự hiển thị trên màn hình, trừ khi được ủy quyền được trao để chia sẻ nội dung đó một cách cụ thể.

async function startCapture() {
  logElem.innerHTML = "";

  try {
    videoElem.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
    dumpOptionsInfo();
  } catch (err) {
    console.error(`Error: ${err}`);
  }
}

Trước khi phát trực tuyến nội dung màn hình đã chụp có thể bắt đầu, tác nhân người dùng sẽ yêu cầu người dùng xác nhận yêu cầu chia sẻ và để chọn nội dung để chia sẻ.

Luồng được kết nối với phần tử

const videoElem = document.getElementById("video");
const logElem = document.getElementById("log");
const startElem = document.getElementById("start");
const stopElem = document.getElementById("stop");

// Options for getDisplayMedia()

const displayMediaOptions = {
  video: {
    cursor: "always"
  },
  audio: false
};

// Set event listeners for the start and stop buttons
startElem.addEventListener("click", (evt) => {
  startCapture();
}, false);

stopElem.addEventListener("click", (evt) => {
  stopCapture();
}, false);
4 bằng cách lưu trữ
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 được trả về vào phần tử ____70.

Hàm

function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
1 mà chúng ta sẽ xem xét trong một khoảnh khắc thông tin về thông tin về luồng vào hộp nhật ký cho mục đích giáo dục.

Nếu bất kỳ điều đó thất bại, mệnh đề

function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
2 sẽ xuất ra một thông báo lỗi cho hộp nhật ký.

Dừng hiển thị chụp

Phương thức

function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
3 được gọi khi nhấp vào nút "Dừng bắt". Nó dừng luồng bằng cách nhận danh sách theo dõi của nó bằng cách sử dụng
function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
4, sau đó gọi phương thức
function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
5 của mỗi bản nhạc. Khi đã hoàn thành,
function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
0 được đặt thành
function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
7 để đảm bảo rằng bất cứ ai quan tâm rằng không có luồng kết nối.

function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}

Thông tin cấu hình bán phá giá

Đối với các mục đích thông tin, phương thức

const gdmOptions = {
  video: true,
  audio: true
}
2 được hiển thị ở trên gọi một phương thức có tên
function stopCapture(evt) {
  let tracks = videoElem.srcObject.getTracks();

  tracks.forEach((track) => track.stop());
  videoElem.srcObject = null;
}
9, sẽ xuất ra các cài đặt theo dõi hiện tại cũng như các ràng buộc được đặt trên luồng khi nó được tạo.

function dumpOptionsInfo() {
  const videoTrack = videoElem.srcObject.getVideoTracks()[0];

  console.info("Track settings:");
  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
  console.info("Track constraints:");
  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
}

Danh sách theo dõi có được bằng cách gọi

function dumpOptionsInfo() {
  const videoTrack = videoElem.srcObject.getVideoTracks()[0];

  console.info("Track settings:");
  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
  console.info("Track constraints:");
  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
}
0 trên
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
5 của màn hình đã bị bắt. Các cài đặt hiện có hiệu lực bằng cách sử dụng
function dumpOptionsInfo() {
  const videoTrack = videoElem.srcObject.getVideoTracks()[0];

  console.info("Track settings:");
  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
  console.info("Track constraints:");
  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
}
2 và các ràng buộc đã được thiết lập được nhận với
function dumpOptionsInfo() {
  const videoTrack = videoElem.srcObject.getVideoTracks()[0];

  console.info("Track settings:");
  console.info(JSON.stringify(videoTrack.getSettings(), null, 2));
  console.info("Track constraints:");
  console.info(JSON.stringify(videoTrack.getConstraints(), null, 2));
}
3

HTML

HTML bắt đầu với một đoạn giới thiệu đơn giản, sau đó vào phần thịt của mọi thứ.

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>

Các phần chính của HTML là:

  1. A
  2. Một nút thứ hai, "Dừng bắt", khi được nhấp vào cuộc gọi
    function stopCapture(evt) {
      let tracks = videoElem.srcObject.getTracks();
    
      tracks.forEach((track) => track.stop());
      videoElem.srcObject = null;
    }
    
    3 để chấm dứt chụp nội dung màn hình.
  3. Một
    const videoElem = document.getElementById("video");
    const logElem = document.getElementById("log");
    const startElem = document.getElementById("start");
    const stopElem = document.getElementById("stop");
    
    // Options for getDisplayMedia()
    
    const displayMediaOptions = {
      video: {
        cursor: "always"
      },
      audio: false
    };
    
    // Set event listeners for the start and stop buttons
    startElem.addEventListener("click", (evt) => {
      startCapture();
    }, false);
    
    stopElem.addEventListener("click", (evt) => {
      stopCapture();
    }, false);
    
    4 trong đó nội dung màn hình bị bắt được phát trực tuyến.
  4. Một khối
    console.log = (msg) => logElem.innerHTML += `${msg}<br>`;
    console.error = (msg) => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
    console.warn = (msg) => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
    console.info = (msg) => logElem.innerHTML += `<span class="info">${msg}</span><br>`;
    
    4 trong đó văn bản đã đăng nhập được đặt bằng phương thức
    console.log = (msg) => logElem.innerHTML += `${msg}<br>`;
    console.error = (msg) => logElem.innerHTML += `<span class="error">${msg}</span><br>`;
    console.warn = (msg) => logElem.innerHTML += `<span class="warn">${msg}<span><br>`;
    console.info = (msg) => logElem.innerHTML += `<span class="info">${msg}</span><br>`;
    
    3 bị chặn.

CSS

CSS hoàn toàn là mỹ phẩm trong ví dụ này. Video được cung cấp một đường viền và chiều rộng của nó được đặt để chiếm gần như toàn bộ không gian ngang có sẵn (

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
0).
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
1 được đặt thành
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
2 để đặt giới hạn trên tuyệt đối trên kích thước của video,

Các lớp

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
3,
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
4 và
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
5 được sử dụng để tạo kiểu cho các loại đầu ra bảng điều khiển tương ứng.

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
0

Kết quả

Sản phẩm cuối cùng trông như thế này. Nếu trình duyệt của bạn hỗ trợ API chụp màn hình, nhấp vào "Bắt đầu bắt" sẽ trình bày giao diện của tác nhân người dùng để chọn màn hình, cửa sổ hoặc tab để chia sẻ.

Bảo vệ

Để hoạt động khi chính sách tính năng được bật, bạn sẽ cần sự cho phép

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
6. Điều này có thể được thực hiện bằng cách sử dụng tiêu đề
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
7 HTTP hoặc nếu bạn đang sử dụng API chụp màn hình trong
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
8, thuộc tính
function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
00 của phần tử
<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
8.

Ví dụ: dòng này trong các tiêu đề HTTP sẽ bật API chụp màn hình cho tài liệu và bất kỳ phần tử

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
8 được nhúng nào được tải từ cùng một nguồn gốc:

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
1

Nếu bạn đang thực hiện chụp màn hình trong

<p>
  This example shows you the contents of the selected part of your display.
  Click the Start Capture button to begin.
</p>

<p>
  <button id="start">Start Capture</button>&nbsp;<button id="stop">
    Stop Capture
  </button>
</p>

<video id="video" autoplay></video>
<br />

<strong>Log:</strong>
<br />
<pre id="log"></pre>
8, bạn có thể yêu cầu quyền chỉ cho khung đó, rõ ràng là an toàn hơn so với yêu cầu quyền chung hơn:

function startCapture(displayMediaOptions) {
 return navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
    .catch((err) => { console.error(`Error:${err}`); return null; });
}
2

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

JavaScript có thể ghi âm thanh không?

API này cung cấp chức năng để ghi lại phương tiện như âm thanh hoặc video. Nó được tạo bằng hàm tạo mediareCorder (). Ở đây chúng tôi sử dụng một thuộc tính của JavaScript, là thuộc tính MediaDevices, được sử dụng để có quyền truy cập vào các thiết bị phương tiện đầu vào được kết nối như micrô, webcam, v.v.. It is created using the MediaRecorder() constructor. Here we use one property of JavaScript, which is mediaDevices property, which is used to get access to connected input media devices like microphones, webcams, etc.

JavaScript có thể phát hiện ảnh chụp màn hình không?

Vì vậy, họ có thể kích hoạt một sự kiện ghi lại khi ai đó nhấn nút màn hình in trên một trang của trang web của họ.Loại phát hiện này được thực hiện bằng cách sử dụng JavaScript để phát hiện nếu bạn đang nhấn nút PrintScreen hoặc các kết hợp nút tắt ảnh chụp màn hình khác trên bàn phím của bạn.This type of detection is accomplished by using javascript to detect if you are pressing the PrintScreen button or other screenshot shortcut button combinations on your keyboard.

RecordRTC là gì?

RecordRTC là thư viện JavaScript WEBRTC cho âm thanh/video cũng như ghi hoạt động màn hình.Nó hỗ trợ Chrome, Firefox, Opera, Android và Microsoft Edge.Nền tảng: Linux, Mac và Windows.WebRTC JavaScript library for audio/video as well as screen activity recording. It supports Chrome, Firefox, Opera, Android, and Microsoft Edge. Platforms: Linux, Mac and Windows.

Các trang web có thể phát hiện phản chiếu?

Vì vậy, theo nghĩa này, câu trả lời là không;Các trang web không thể phát hiện các hoạt động của người dùng như chụp màn hình.Tuy nhiên, nếu trang web có bạn tải xuống một số phần mềm và cài đặt nó, nó có thể nhận ra các công cụ chụp màn hình bên ngoài đang được sử dụng trên các trang web của họ (nếu họ muốn).websites cant detect user activities like screen capture. However, if the website has you download some software and install it, it can most probably recognize external screen capture tools being used on their web pages (if they want to).