Spotify and Google Music allows a user to play music in background allowing also the media notification to be dismissible.
When you move one of these apps to background, then pause the playback, you are able to dismiss the media notification. However if you don’t dismiss the notification, so it is left in paused state for long time, the System won’t kill the “audio service” as the notification is always visible and immediately react to play/pause.
As far as I know it is not possible using foreground services as in order to dismiss the notification, we’d need to call stopForeground(false), which would let system to kill the service.
Here is how we do it right now, in our app:
When playback is started, we run MusicService as a foreground service with following code:
Then, when user leave the app, he is able to control the playback via media notification. As the notification is tightly coupled with the MusicService, user won’t be able to dismiss it until we call:
That’s why we call this method when user press pause button.
However this makes a Service background, so system will kill it in short period of time.
How Spotify and Google Music could workaround it? What is the way to achieve it?