Skip to content

Update MediaSessionManager to return priority order of sessions as shown by System UI #171

Open

Description

@entibaldi

In Media3 there is no class/mechanism equivalent of a MediaSessionManager?
If I want my app to control the media session that another app is running, the only way is to convert android.media.session.MediaSession.Token (obtained by the system MediaSessionManager) to MediaSessionCompat.Token then to convert it again to androidx.media3.session.Token? Or did I miss something?

Activity

  1. self-assigned this
    on Sep 21, 2022
  2. tonihei commented on Sep 21, 2022

    @tonihei
    Collaborator

    This is more of less correct when going from a platform android.media.session.MediaSession.Token to a media3 session token.

    There is already a method to convert the MediaSessionCompat.Token to the media3 token:

    public static ListenableFuture<SessionToken> createSessionToken(

    Would it help to have a similar method converting android.media.session.MediaSession.Token to a media3 token?

    Not sure if relevant to your use case, but it's also possible to directly query all existing media3 sessions associated with a service using this method:

    public static ImmutableSet<SessionToken> getAllServiceTokens(Context context) {

    Note that this list doesn't include sessions that are not associated to a service (MediaSessionService or MediaLibraryService or MediaBrowserServiceCompat).

  3. entibaldi commented on Sep 21, 2022

    @entibaldi
    Author

    If the idea of the media3 library is to progressively phase out the need of the other older media support library, I think it would be good to have a method to directly convert android.media.session.MediaSession.Token to a media3 session token. This way there wouldn't be a need to have a direct dependency to both libraries from our app.

    I saw the getAllServiceTokens method but it's not clear to me how to distinguish which token is the one currently active, and like you mentioned we might be missing some sessions in that case. We are also running into SecurityExceptions when trying to create MediaControllers from these tokens.

    To explain better our use case, we have a bluetooth companion app, and our users are able to control music played on the Android phone, directly from the watches. Our Android app forwards the media session information from Android to the watches and the inverse is true for the commands (play/pause/skip etc.). We aim to have an implementation that works with as many Android music/media applications as possible.

  4. tonihei commented on Sep 21, 2022

    @tonihei
    Collaborator

    I think it would be good to have a method to directly convert android.media.session.MediaSession.Token to a media3 session token.

    Agree, I'll convert this to an enhancement.

    I saw the getAllServiceTokens method but it's not clear to me how to distinguish which token is the one currently active, and like you mentioned we might be missing some sessions in that case.

    That's interesting. I wonder if we need the equivalent of MediaSessionManager.getActiveSessions() that returns an ordered list with the most relevant session listed first. @marcbaechinger What's your take on this?

    We are also running into SecurityExceptions when trying to create MediaControllers from these tokens.

    Isn't this expected if you don't have permission to access these sessions?

  5. changed the title [-]How to correctly acquire ongoing media session token from another app?[/-] [+]Add helper method to convert platform MediaSession.Token to media3 session Token[/+] on Sep 21, 2022
  6. entibaldi commented on Sep 21, 2022

    @entibaldi
    Author

    We are also running into SecurityExceptions when trying to create MediaControllers from these tokens.

    Isn't` this expected if you don't have permission to access these sessions?

    We have permissions to access notifications and we can trigger theirs actions. Controlling any app through the session tokens retrieved from the MediaSessionManager seems to be working fine.

  7. added a commit that references this issue on Jun 22, 2023
  8. added a commit that references this issue on Oct 21, 2025
  9. tonihei commented on Jun 30, 2026

    @tonihei
    Collaborator

    There is a helper method to convert a platform token to a Media3 token and we now have a MediaSessionManager class in Media3 to provide direct access to Media3 tokens of all sessions (assuming your app has the right permissions).

    I'll leave this issue open to track the idea of returning an ordered list sorted by the priority SysUI applies to sessions for a more consistent view on the data.

  10. changed the title [-]Add helper method to convert platform MediaSession.Token to media3 session Token[/-] [+]Update MediaSessionManager to return priority order of sessions as shown by System UI[/+] on Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions