Skip to content

Commit

Permalink
Enter Picture-in-Picture automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Dec 16, 2024
1 parent 0d056b1 commit 2abc188
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions media-session/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,37 @@ try {
log('Warning! The "seekto" media session action is not supported.');
}

/* Picture-in-Picture Canvas */

const canvas = document.createElement('canvas');
canvas.width = canvas.height = 512;

const video = document.createElement('video');
video.srcObject = canvas.captureStream();
video.muted = true;

async function showPictureInPictureWindow() {
const image = new Image();
image.crossOrigin = true;
image.src = [...navigator.mediaSession.metadata.artwork].pop().src;
await image.decode();

canvas.getContext('2d').drawImage(image, 0, 0, 512, 512);
await video.play();
await video.requestPictureInPicture();
}

/* Enter Picture-in-Picture automatically (supported since Chrome 132) */

try {
navigator.mediaSession.setActionHandler('enterpictureinpicture', function() {
log('> Automatically enter picture-in-picture.');
showPictureInPictureWindow();
});
} catch(error) {
log('Warning! The "enterpictureinpicture" media session action is not supported.');
}

/* Utils */

function getAwesomePlaylist() {
Expand Down

0 comments on commit 2abc188

Please sign in to comment.