androidx / androidx/media

MediaLibraryService.onTaskRemoved() not called when app terminated without the service being started

Open
#817 4 comments 1 reaction 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Nov 16, 2023.

bug
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

Version

Media3 1.1.1

More version details

Version Media3 1.2.0-beta01

Devices that reproduce the issue

Pixel 3A running Android 11 Api 30

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Yes

Reproduction steps
  1. Launch an app that prepare an audio without playing it (playWhenReady = false)
  2. Remove the app from the recent list
Expected result

The onTaskRemoved() function is called

Actual result

The onTaskRemoved() function is not called.

After investigation, i could have the problem solved passing the service in foreground and then setting it back to background. Something (i don't know what) allow the onTaskRemoved() called correctly when the service is set once in foreground.

Digging in this problem, i also notice that playing/pausing and audio from the app was posting the notification with the wrong value for startInForegroundRequired (false when playing and true when pausing).
This is causing the onDestroy() not called after stopSelf() is called in onTaskRemoved() when the audio is active and is paused before stopSelf().
This problem is caused by this function from MediaNotificationManager

boolean shouldRunInForeground(
      MediaSession session, boolean startInForegroundWhenPaused) {
    @Nullable MediaController controller = getConnectedControllerForSession(session);
    return controller != null
        && (controller.getPlayWhenReady() || startInForegroundWhenPaused)
        && (controller.getPlaybackState() == Player.STATE_READY
            || controller.getPlaybackState() == Player.STATE_BUFFERING);
  }

The getConnectedControllerForSession(session) always return the controller for the media notification, controller.getPlayWhenReady() is not yet updated and so the old value is returned (false when calling play() from an other controller, and true when calling pause()

Media

Every media

Bug Report
  • You will email the zip file produced by adb bugreport to android-media-github@google.com after filing this issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.