Skip to content

Commit

Permalink
Handle enterpictureinpicture media session action
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Oct 16, 2023
1 parent 2bb27d9 commit 1de1b35
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions media-session/video-conferencing.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h3>Background</h3>
<video playsinline autoplay muted></video>
<button id="openCameraButton">Open Camera</button>
<button id="togglePipButton">Toggle Picture-in-Picture</button>
<input id="autoPipCheckbox" type="checkbox">Automatically enter Picture-in-Picture</button>

{% include output_helper.html %}

Expand Down
17 changes: 17 additions & 0 deletions media-session/video-conferencing.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ togglePipButton.addEventListener("click", async () => {
}
});

autoPipCheckbox.addEventListener("input",g () => {
try {
if (!autoPipCheckbox.checked) {
// Disable automatically enter Picture-in-Picture.
navigator.mediaSession.setActionHandler("enterpictureinpicture", null);
return;
}
// Request browser to automatically enter Picture-in-Picture when eligible.
navigator.mediaSession.setActionHandler("enterpictureinpicture", async () => {
log('> Video is eligible for Automatic Picture-in-Picture.');
await video.requestPictureInPicture();
});
} catch (error) {
log('Warning! The "enterpictureinpicture" media session action is not supported.');
}
});

try {
navigator.mediaSession.setActionHandler('togglemicrophone', () => {
log('> User clicked "Toggle Mic" icon.');
Expand Down
12 changes: 12 additions & 0 deletions media-session/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ try {
log('Warning! The "seekto" media session action is not supported.');
}


/* Enter Picture-in-Picture (supported since Chrome 120) */

try {
navigator.mediaSession.setActionHandler('enterpictureinpicture', async function(event) {
log('> User clicked "Enter Picture-in-Picture" icon.');
await video.requestPictureInPicture();
});
} catch(error) {
log('Warning! The "enterpictureinpicture" media session action is not supported.');
}

/* Utils */

function getAwesomePlaylist() {
Expand Down

0 comments on commit 1de1b35

Please sign in to comment.