var trackElement = document.getElementById("track_el");
// Make sure browser has Media Session API available
if ('mediaSession' in navigator) {
// Access to Media Session API
var ms = navigator.mediaSession;
// Create track info JSON variable
var trackInfo = {};
// Set track title
trackInfo.title = "Polaris";
// Set artist name
trackInfo.artist = "Downtown Binary & The Present Sound";
// Set album name
trackInfo.album = "Umbra";
// Set album art (NOTE: image files must be hosted in "http" or "https" protocol to be shown)
trackInfo.artwork = [
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_96.jpg', sizes: '96x96', type: 'image/jpg' },
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_128.jpg', sizes: '128x128', type: 'image/jpg' },
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_192.jpg', sizes: '192x192', type: 'image/jpg' },
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_256.jpg', sizes: '256x256', type: 'image/jpg' },
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_384.jpg', sizes: '384x384', type: 'image/jpg' },
{ src: 'https://antonyhr.neocities.org/temp/polaris/polaris_album_art_512.jpg', sizes: '512x512', type: 'image/jpg' }
];
// Then, we create a new MediaMetadata and pass our trackInfo JSON variable
var mediaMD = new MediaMetadata(trackInfo);
// We assign our mediaMD to MediaSession.metadata property
ms.metadata = mediaMD
// And that will be all for show our custom track info in Windows (or any supported) Media Player Pop-Up
// If we need to customize Media controls, we must set action handlers (NOTE: It's not necessary to add all action handlers).
ms.setActionHandler('play', function() {
trackElement.play();
var trackInfoEl = document.getElementById("track_info_el");
trackInfoEl.textContent = "Track is playing.";
});
ms.setActionHandler('pause', function() {
trackElement.pause();
var trackInfoEl = document.getElementById("track_info_el");
trackInfoEl.textContent = "Track is paused.";
});
ms.setActionHandler('stop', function() { /* Code excerpted. */ });
ms.setActionHandler('seekbackward', function() { /* Code excerpted. */ });
ms.setActionHandler('seekforward', function() { /* Code excerpted. */ });
ms.setActionHandler('seekto', function() { /* Code excerpted. */ });
ms.setActionHandler('previoustrack', function() { /* Code excerpted. */ });
ms.setActionHandler('nexttrack', function() { /* Code excerpted. */ });
} else {
console.warn("Your browser doesn't have Media Session API");
}
body {background: #181818;}
p {color: #fff; font-family: "Verdana", "Roboto", "Century Gothic";}
<audio id="track_el" controls autoplay src="https://firebasestorage.googleapis.com/v0/b/tempfiles-2912.appspot.com/o/polaris_clip.mp3?alt=media&token=bcfc245e-dc89-4046-b287-b99046c786bf" type="audio/mp3"></audio>
<p id="track_info_el">Track is playing.</p>
<p>Once audio is playing, see Media Player Pop-Up.</p>