androidx / androidx/media

Completely stopping service when user swipes away media notification.

Open
#1,276 2 comments 1 reaction 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Apr 15, 2024.

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

Description

This subject has been touched indirectly here, but since I couldn't find a feature request for this, I am formally creating one.

When user closes the app (swipes it away from "recent tasks" list), developer has 2 behavior choices:

  1. Stop playback and destroy service (that is, MediaLibraryService/MediaSessionService). ex. Spotify does this.
  2. Continue playback in background which will be controlled with "MediaNotificationController" (or "AndroidAutoController").

First behavior is easy.
This makes sure the onDestroy is called where we can properly clean everything else that needs clearing:

@Override
public void onTaskRemoved(Intent rootIntent) {
    if (mediaLibrarySession != null) {
        mediaLibrarySession.getPlayer().release();
        mediaLibrarySession.release();
        mediaLibrarySession = null;
    }

    stopSelf();
}

The second behavior requires 3 things to work/behave properly:

2.1. Service needs to be in/out foreground state when there is/isn't an active playback. "media3" handles this for us already.

2.2. In "onTaskRemoved" we have to stop service if currently there is no active playback, so service would die with app:

Player player = mediaLibrarySession.getPlayer();
if (!player.getPlayWhenReady() || player.getMediaItemCount() == 0) {
    if (mediaLibrarySession != null) {
        mediaLibrarySession.getPlayer().release();
        mediaLibrarySession.release();
        mediaLibrarySession = null;
    }

    stopSelf();
}

2.3. This is where the problem is. When app is closed, user stops playback (ex. from media notification) and user swipes away media notification - we have to destroy the service.
This destruction is required, because usually developers service will do more than just playing music.
For an example. it keeps polling livestream song data from API, records some analytics etc.
This needs to be promptly stopped when user swipes away media notification, so these extra actions wouldn't needlessly drain users battery or bombard developers API.

Currently this is extremely difficult to do, because by default "media3" uses DefaultMediaNotificationProvider, which has a hardcoded logic that cannot be overridden:

.setDeleteIntent(actionFactory.createMediaActionPendingIntent(mediaSession, COMMAND_STOP))

The only way we can customize it currently is to make our own MediaNotification.Provider, but that is a great undertaking.
We (devs) don't want to handle the various logic found in DefaultMediaNotificationProvider ourselves, we want it to be done by the library.

Suggestion:
When developer creates his own CustomMediaNotificationProvider which extends DefaultMediaNotificationProvider, give us a callback in there which either gets called when user swipes away media notification, or allows us to set our own pending intent.

In my opinion, this is quite an oversight and should be addressed with higher priority than other enhancements.

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.