Media Session API

To let users know what's currently playing in their browser and control it without returning to the page that launched it, the Media Session API has been introduced:


Play the audio. Then try clicking the pause button on your keyboard.
RESETRUNFULL
<!DOCTYPE html><html><body>
<audio src="/shared/music.mp3" controls/>
<script>
if ('mediaSession' in navigator) {
   navigator.mediaSession.metadata = new MediaMetadata({
      title: 'Unforgettable',
      artist: 'Nat King Cole',
      album: 'The Ultimate Collection (Remastered)',
      artwork: [
        { src: 'https://dummyimage.com/96x96'  , sizes: '96x96',   type: 'image/png' },
        { src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
        { src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
        { src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
        { src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
        { src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
      ]
   });
   let nm = navigator.mediaSession;
   nm.setActionHandler('play', handler);
   nm.setActionHandler('pause', handler);
   nm.setActionHandler('stop', handler);
   nm.setActionHandler('seekbackward', handler);
   nm.setActionHandler('seekforward', handler);
   nm.setActionHandler('seekto', handler);
   nm.setActionHandler('previoustrack', handler);
   nm.setActionHandler('nexttrack', handler);
}
function handler(details) {
   const audio = document.querySelector("audio");
   switch(details.action) {
      case "play":
         alert("The music is going to be resumed!");
         audio.play();
         break;      
      case "pause":
         alert("The music has been paused!");
         audio.pause();
         break;      
      case "seekforward":
         audio.currentTime = Math.min(audio.currentTime + details.seekOffset, audio.duration);
         break;
      case "seekbackward":
         audio.currentTime = Math.max(audio.currentTime - details.seekOffset, 0);
         break;
   }
   /*
       ......details.fastSeek
       ......details.seekTime
   */
}
</script>
</body></html>