Last active
May 12, 2021 17:10
-
-
Save Patrick-web/118f63345b24fa92d3f8f104a725a2b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionHandlers = [ | |
['play', () => { /* ... */ }], | |
['pause', () => { /* ... */ }], | |
['previoustrack', () => { /* ... */ }], | |
['nexttrack', () => { /* ... */ }], | |
['stop', () => { /* ... */ }], | |
['seekbackward', (details) => { /* ... */ }], | |
['seekforward', (details) => { /* ... */ }], | |
['seekto', (details) => { /* ... */ }], | |
/* Video conferencing actions */ | |
['togglemicrophone', () => { /* ... */ }], | |
['togglecamera', () => { /* ... */ }], | |
['hangup', () => { /* ... */ }], | |
]; | |
for (const [action, handler] of actionHandlers) { | |
try { | |
navigator.mediaSession.setActionHandler(action, handler); | |
} catch (error) { | |
console.log(`The media session action "${action}" is not supported yet.`); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After media (video or audio) starts playing | |
await document.querySelector("video").play(); | |
if ("mediaSession" in navigator) { | |
navigator.mediaSession.metadata = new MediaMetadata({ | |
title: 'Never Gonna Give You Up', | |
artist: 'Rick Astley', | |
album: 'Whenever You Need Somebody', | |
artwork: [ | |
{ src: 'https://via.placeholder.com/96', sizes: '96x96', type: 'image/png' }, | |
{ src: 'https://via.placeholder.com/128', sizes: '128x128', type: 'image/png' }, | |
{ src: 'https://via.placeholder.com/192', sizes: '192x192', type: 'image/png' }, | |
{ src: 'https://via.placeholder.com/256', sizes: '256x256', type: 'image/png' }, | |
{ src: 'https://via.placeholder.com/384', sizes: '384x384', type: 'image/png' }, | |
{ src: 'https://via.placeholder.com/512', sizes: '512x512', type: 'image/png' }, | |
] | |
}); | |
// TODO: Update playback state. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Allow users to control media playback and get notified