Comment by wonger_
8 months ago
You should! The browser APIs are straightforward:
navigator.mediaSession.metadata = new MediaMetadata({
title: song.name,
album: song.category,
artwork: [{src: song.imagePath, type: 'image/jpg'}]
})
navigator.mediaSession.setActionHandler('play', player.play)
navigator.mediaSession.setActionHandler('pause', player.pause)
navigator.mediaSession.setActionHandler('nexttrack', player.nextTrack)
navigator.mediaSession.setActionHandler('previoustrack', player.prevTrack)
// song and player are instances of state
Then you get those native media controls. Even stuff like "hey google, play/pause/skip"
Does it work on iOS too?
Yes: https://caniuse.com/mdn-api_navigator_mediasession
Try using Spotify's mobile web app for an example. Works great.
MDN says yes as of iOS 15. Don't have an iPhone to double check atm.