-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
40 lines (36 loc) · 1.19 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const videoElement = document.getElementById("video");
const button = document.getElementById("button");
const buttonContainer = document.getElementById("button-container");
// Prompt to select media stream and pass it to the video element, then play
async function selectMediaStream() {
try {
const mediaStream = await navigator.mediaDevices.getDisplayMedia();
videoElement.srcObject = mediaStream;
videoElement.onloadedmetadata = () => {
videoElement.play();
};
} catch (error) {
// Catch error here
console.log("Whoops, error here: ", error);
}
}
button.addEventListener("click", async () => {
if (document.pictureInPictureElement) {
// Leave picture in picture
document.exitPictureInPicture();
} else {
// Disable the button while waiting for picture in picture
button.disabled = true;
// Start Picture in Picture
await videoElement.requestPictureInPicture();
// Enable the button and make it a stop button
button.disabled = false;
button.innerText = "STOP";
}
});
// Restore the start button when leaving picture in picture
videoElement.onleavepictureinpicture = () => {
button.innerText = "START";
};
// On load
selectMediaStream();