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.71k stars 409 forks source link

onTaskRemoved not called when app dismissed from recent tasks on Android 14 #805

Open svenoaks opened 1 year ago

svenoaks commented 1 year ago

Version

Media3 1.1.1

More version details

Also happens with 1.2.0rc-1

Devices that reproduce the issue

Devices that do not reproduce the issue

Reproducible in the demo app?

Yes

Reproduction steps

  1. Play some audio in demo-session app
  2. Tap pause in the app
  3. Navigate to the home screen
  4. Swipe up on the demo-session app from recent apps
  5. Observe dead notification in notifications

Expected result

There is no notification in the notification drawer

Actual result

There is a notification in the notification drawer that is inoperable, if you click on play button, a DeadObjectException is thrown

Media

Jazz & Blues, Jazz in Paris

Bug Report

svenoaks commented 1 year ago

https://github.com/androidx/media/assets/5206058/90f40abb-737e-4ea5-9cdf-36ef69a987d2

Here's video of the bug occurring, perhaps an important reproduction step is you have to navigate back to the home screen before going to the recent apps list, otherwise it doesn't seem to occur

marcbaechinger commented 1 year ago

Yeah, true. I can repro this. Took a while to make it repro but I was able to see that twice now. Thanks for reporting. We look into this.

marcbaechinger commented 10 months ago

This is a bug in Android 14. We have reported this internally and a fix is on it's way. There is nothing Media3 as a library or an app could do about it.

leejooheon commented 9 months ago

It has been fixed to the security patch version of 2024-02-05, Android14, Pixel6

It's my mistake. sorry. This bug still exists.

marcbaechinger commented 9 months ago

Thanks for the heads up. Can you give us the build number with which you have verified this has been fixed and how you tried to repro?

When I test with UQ1A.240205.004 I still see the problem I'm afraid.

jigarpandya-techholding commented 9 months ago

@svenoaks @marcbaechinger I'm facing this issue on Android 13 as well. The work around that I have implemented as @marcbaechinger described in one of the comments is to release the player and session before calling stopSelf in onTaskRemoved. I just wanted to confirm whether this issue is only occurring when player is paused ? as when player is playing and I remove app from recents, it works fine.

marcbaechinger commented 9 months ago

I think these are two different issues.

On Android 14 and with the repro steps from above, onTaskRemoved isn't called at all.

I just wanted to confirm whether this issue is only occurring when player is paused ? as when player is playing and I remove app from recents, it works fine.

Pausing the player to repro this issue is required, because that is how the demo app is implemented. Without pausing, playback would continue in the background when dismissing the app from recents which is working as intended.

Generally, to stop the service the service must not run in the foreground (hence the player needs to be paused) and no controller/browser must be bound to the service (options: all controller disconnected already or the session is released). If either of these conditions are violated, calling stopSelf is a no-op.

jigarpandya-techholding commented 9 months ago

@marcbaechinger thanks for the clarity. the two conditions that you mention about stopping the service make sense. In my case, even on paused state, on dismissing the app doesn't remove the notification implies that any controller is not disconnected yet ? then trying to play the media from notification results in ForegroundServiceDidNotStartInTimeException.

marcbaechinger commented 9 months ago

I'd guess so yes, but I can't know with the available information. #1059 is about the same thing and may be interesting for you.

nift4 commented 9 months ago

I was able to work around this issue with this code (simplified version):

         handler.post {
            if (willDie) {
                if (Build.VERSION.SDK_INT != Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                    throw NullPointerException("this code path should never be reachable")
                }
                // Android 14 bug.
                // https://github.com/androidx/media/issues/805
                // In Android 13:
                // 1. Service starts
                // 2. UI thread moves on to this call
                // 3. Some time later, user swipes away app (while music paused)
                // 4. onTaskRemoved() calls stopSelf()
                // 5. onDestroy() called, happy ending
                // In Android 14:
                // 1. Service starts
                // 2. UI thread moves on to this call
                // 3. Some time later, user swipes away app (while music paused)
                // 4. App process killed, notification abandoned
                // 5. App process restarted
                // 6. instantly, onTaskRemoved() calls stopSelf()
                // 7. This is called (and because we are after onTaskRemoved, willDie is true)
                // 8. onDestroy() called
                // To workaround this, we cancel notification here and let service die peacefully
                val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                nm.cancel(DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID)
                return@post
            }
       }

in onCreate()

and this in onTaskRemoved():

    override fun onTaskRemoved(rootIntent: Intent?) {
        if (!mediaSession!!.player.playWhenReady || mediaSession!!.player.mediaItemCount == 0) {
            willDie = true // a14 bug workaround
            stopSelf()
        }
    }
patryk84a commented 1 month ago

Jeśli mi się uda, obejść ten problem, za pomocą wyjaśnionego kodu (wersja uproszczona):

         handler.post {
            if (willDie) {
                if (Build.VERSION.SDK_INT != Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                    throw NullPointerException("this code path should never be reachable")
                }
                // Android 14 bug.
                // https://github.com/androidx/media/issues/805
                // In Android 13:
                // 1. Service starts
                // 2. UI thread moves on to this call
                // 3. Some time later, user swipes away app (while music paused)
                // 4. onTaskRemoved() calls stopSelf()
                // 5. onDestroy() called, happy ending
                // In Android 14:
                // 1. Service starts
                // 2. UI thread moves on to this call
                // 3. Some time later, user swipes away app (while music paused)
                // 4. App process killed, notification abandoned
                // 5. App process restarted
                // 6. instantly, onTaskRemoved() calls stopSelf()
                // 7. This is called (and because we are after onTaskRemoved, willDie is true)
                // 8. onDestroy() called
                // To workaround this, we cancel notification here and let service die peacefully
                val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                nm.cancel(DefaultMediaNotificationProvider.DEFAULT_NOTIFICATION_ID)
                return@post
            }
       }

w onCreate()

i do w onTaskRemoved():

    override fun onTaskRemoved(rootIntent: Intent?) {
        if (!mediaSession!!.player.playWhenReady || mediaSession!!.player.mediaItemCount == 0) {
            willDie = true // a14 bug workaround
            stopSelf()
        }
    }

On Android 14 in Chinese POCO, the problem of not calling onTaskRemoved still exists. When playback is stopped and the app is removed from the recent list, the onTaskRemoved method is not called at all. That's why your fix won't work, because onTaskRemoved is not called. I wanted to solve the problem by adding a method to my activity in the onDestroy event that removes MediaItems from the Player. Then the service would go to the background and be killed after some time. I just don't know why it doesn't work:

 @Override
  public void onDestroy() {
    if(!mediaBrowser.isPlaying()) {
      mediaBrowser.clearMediaItems();
      mediaBrowser.release();
    }
    MediaBrowser.releaseFuture(browserFuture);  
  }

Edit: Instead of onDestroy I used onStop and the code started working. When playback is stopped and the application goes to the background, MediaItems are removed. The notification disappears, the service goes to the background. After a while, the service is killed by the system.

lovegaoshi commented 1 month ago

I just spent a frustrating day on this exact issue, and https://github.com/androidx/media/issues/843#issuecomment-1860555950 resolved it for me. as a result I see a gazillion onStartCommand calls with a null intent, but I guess its not too bad.

I've followed most stale notification issues and its similar to another fella (I cant find anymore) with the same Scheduling restart of crashed service. I'm deducing that it might be related to a onTrimMemory(40) event, but really no clue