w3c/mediasession

Use within a PWA context / Service Worker to replicate native audio apps?

Opened this issue ยท 7 comments

If one wants to create a Progressive Web App that mimics Spotify or one of the many podcasts apps out there, then currently one needs to have ones app be a Single Page Application, as neither the Media Session API nor the Web Audio API is available in a worker, they all expects the web page context to stay alive.

There is an open issue about making the Web Audio API available to Workers: https://github.com/WebAudio/web-audio-api/issues/1098

And the Media Session API would be the natural UI for such a background playback.

In #48 support for the Web Audio API was added, which meant that as long as https://github.com/WebAudio/web-audio-api/issues/1098 was solved then the Media Session API would likely be available for any AudioContext created within a Service Worker (or other supported Worker)

However, as noted in w3c/audio-session#11, the addition from #48 was dropped in #135 so currently either that direct support for AudioContext would need to be restored for the Media Session API to be a usable interface for interacting with audio playing in workers โ€“ or the Media Session API would have to define its own availability within either Service Workers specifically or Workers in general โ€“ else it will stay impossible to fully mimic a native audio app as a PWA.

Could it be that the removal was caused by the split of this spec into the Media Session API spec and the Audio Focus API spec (https://github.com/WICG/audio-focus)?

Been asking around on Twitter about this over time, here's link to some of those discussions:

Could it be that the removal was caused by the split of this spec into the Media Session API spec and the Audio Focus API spec (https://github.com/WICG/audio-focus)?

Correct.

@voxpelli is this what you described here will make me able to play two audio streams simultaneously, like for example, Spotify and my podcast PWA?

@NavyCoat Not really, itโ€™s about controlling the playback in the background without relying on a window being open to play it.

A desktop app like Spotify is able to continue play even after you have closed its window, but that is currently not possible for a PWA as it can only do playback from within its HTML page, not from within its background service worker.

Connecting this to some other issues about,

Linking this issue to whatwg/html#8538 as well

Marking as enhancement for now.

chrisn commented

Background playback is definitely an interesting use case for my organization. We'd like to enable media playback that can continue across same-site navigations, and redesigning as a single-page application would be far too costly.

With various proposals being suggested here (service workers, iframes that carry state between navigations, MediaSession for UI, Web Audio support, etc), it seems to me it's worth looking at how to do this holistically.