Skip to content

Commit 2abc188

Browse files
Enter Picture-in-Picture automatically
1 parent 0d056b1 commit 2abc188

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

media-session/audio.js

+31
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ try {
129129
log('Warning! The "seekto" media session action is not supported.');
130130
}
131131

132+
/* Picture-in-Picture Canvas */
133+
134+
const canvas = document.createElement('canvas');
135+
canvas.width = canvas.height = 512;
136+
137+
const video = document.createElement('video');
138+
video.srcObject = canvas.captureStream();
139+
video.muted = true;
140+
141+
async function showPictureInPictureWindow() {
142+
const image = new Image();
143+
image.crossOrigin = true;
144+
image.src = [...navigator.mediaSession.metadata.artwork].pop().src;
145+
await image.decode();
146+
147+
canvas.getContext('2d').drawImage(image, 0, 0, 512, 512);
148+
await video.play();
149+
await video.requestPictureInPicture();
150+
}
151+
152+
/* Enter Picture-in-Picture automatically (supported since Chrome 132) */
153+
154+
try {
155+
navigator.mediaSession.setActionHandler('enterpictureinpicture', function() {
156+
log('> Automatically enter picture-in-picture.');
157+
showPictureInPictureWindow();
158+
});
159+
} catch(error) {
160+
log('Warning! The "enterpictureinpicture" media session action is not supported.');
161+
}
162+
132163
/* Utils */
133164

134165
function getAwesomePlaylist() {

0 commit comments

Comments
 (0)