'Android media player service lifecycle : service gets killed as soon as paused

I develop a music player for Android, Blade Player. It is capable of playing music from local storage or Spotify.

I have an issue with Android media playing service lifecycle and architecture. I read the Android Developers documentation, for example this.

The issue i have is the following :

For the user to be able to 'swipe out' and delete the media notification, the service must be have called stopForeground(false) (or stopForeground(FLAG_DETACH)). It also makes sense that when the player is paused the service calls stopForeground, as it is not foreground anymore, it is not doing anything, and Android could kill it if it needs more memory.

However, when doing that, on my phone and many others, Android kills the service when staying on another app for more than 1 minute. For example, if you are listening to music using Blade Player, you go on youtube to watch a video, and you want to resume your music at the end of the video : you can't, Android already killed the service to free memory (even tho i have 6Gb RAM on my phone and i only use 3Gb).

Either i am doing something wrong, or i need to change the implementation even if it is not what Google wants us to do, because this is terrible user experience.

By the way, my MediaBrowserService is 'bound' to the activity, but that does not appear to change anything (i guess it is because the activity is foreground so it does not matter).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source