Skip to content

Commit c4f3954

Browse files
Handle enterpictureinpicture media session action
1 parent 2bb27d9 commit c4f3954

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

media-session/video-conferencing.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h3>Background</h3>
1919
<video playsinline autoplay muted></video>
2020
<button id="openCameraButton">Open Camera</button>
2121
<button id="togglePipButton">Toggle Picture-in-Picture</button>
22+
<input id="autoPipCheckbox" type="checkbox">Automatically enter Picture-in-Picture</button>
2223

2324
{% include output_helper.html %}
2425

media-session/video-conferencing.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ togglePipButton.addEventListener("click", async () => {
2727
}
2828
});
2929

30+
autoPipCheckbox.addEventListener('input', () => {
31+
try {
32+
if (!autoPipCheckbox.checked) {
33+
// Disable automatically enter Picture-in-Picture.
34+
navigator.mediaSession.setActionHandler('enterpictureinpicture', null);
35+
return;
36+
}
37+
// Request browser to automatically enter Picture-in-Picture when eligible.
38+
navigator.mediaSession.setActionHandler('enterpictureinpicture', async () => {
39+
log('> Video is eligible for Automatic Picture-in-Picture.');
40+
await video.requestPictureInPicture();
41+
});
42+
} catch (error) {
43+
log('Warning! The "enterpictureinpicture" media session action is not supported.');
44+
}
45+
});
46+
3047
try {
3148
navigator.mediaSession.setActionHandler('togglemicrophone', () => {
3249
log('> User clicked "Toggle Mic" icon.');

media-session/video.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ try {
128128
log('Warning! The "seekto" media session action is not supported.');
129129
}
130130

131+
132+
/* Enter Picture-in-Picture (supported since Chrome 120) */
133+
134+
try {
135+
navigator.mediaSession.setActionHandler('enterpictureinpicture', async function() {
136+
log('> User clicked "Enter Picture-in-Picture" icon.');
137+
await video.requestPictureInPicture();
138+
});
139+
} catch(error) {
140+
log('Warning! The "enterpictureinpicture" media session action is not supported.');
141+
}
142+
131143
/* Utils */
132144

133145
function getAwesomePlaylist() {

0 commit comments

Comments
 (0)