Search code examples
androidweb-audio-apiprogressive-web-appschrome-for-android

Prevent Android from closing a Progressive Web App playing audio in the background


I've written a progressive web app which plays music using the Web Audio API. A common user journey is to start the music playing, then change to other apps whilst still listening to the music. This works fine for a while, until android decides that it needs more resources for the other apps and closes down my PWA without warning.

Is there a way to hint to the browser and OS that despite not being in the foreground, the user is still using a particular web app? I'm fine for it to be tidied up in the background when the music is paused, but not whilst the user is still listening to something.

The main browser I'm interested in is Chrome for Android (though something platform agnostic would be ideal).


Solution

  • You can't, today. This is somewhat related to MediaSessions (https://wicg.github.io/mediasession/), but you really need a background app, which is not a concept that's yet supported by the web. (It's related, also, to ServiceWorkers and Web Workers, but again, not precisely. It's really a difficult scenario, as such an app has a definite ability to drain batteries.)