androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.65k stars 392 forks source link

How to stop the service when the app is dismissed from recent tasks #482

Open popivyurii opened 1 year ago

popivyurii commented 1 year ago

Media3 Version

Media3 1.1.0-alpha01

Devices that reproduce the issue

Pixel 4 API 33 or any device

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Yes

Reproduction steps

Launch the demo-session app. Play any song.

We have this code here

override fun onTaskRemoved(rootIntent: Intent?) {
    If (!player.playWhenReady) {
      stopSelf()
    }
  }

The question is how to close the app when I swipe the app from recents? If I remove this check if (!player.playWhenReady) and call stopSelf() app is leaking. App is not visible in recents but android studio profiler shows that it is running and the service still takes memory.

Adding player.stop() before stopSelf() in onTaskRemoved(rootIntent: Intent?) also not helping. So I'm looking for a solution to close the app and free all resources when swiping from recents or clicking on custom button in notification.

One more question. When killing the app by

val id = Process.myPid()
Process.killProcess(id)

sometimes the service restarts itself. Why does it happen?

Expected result

I need a way to close the app and free memory in both cases when Exoplayer is playing/paused.

Actual result

Demo app release resources only when exoplayer is paused. !player.playWhenReady

Media

Screenshot 2023-06-27 at 13 15 57

Bug Report

marcbaechinger commented 1 year ago

So I'm looking for a solution to close the app and free all resources when swiping from recents or clicking on custom button in notification.

When the app should be stopped when the task is removed:

  override fun onTaskRemoved(rootIntent: Intent?) {
    player.release()
    mediaLibrarySession.release()
    stopSelf()
  }

If the service should continue in background:

override fun onTaskRemoved(rootIntent: Intent?) {
    If (!player.playWhenReady) {
      stopSelf()
    }
  }

sometimes the service restarts itself. Why does it happen?

Because the service returns START_STICKY with onStartCommand().