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.75k stars 418 forks source link

Add helper method to convert platform MediaSession.Token to media3 session Token #171

Open entibaldi opened 2 years ago

entibaldi commented 2 years ago

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?

tonihei commented 2 years ago

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: https://github.com/androidx/media/blob/50475814f700d08519c88585c9583f2aba5d702e/libraries/session/src/main/java/androidx/media3/session/SessionToken.java#L260

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: https://github.com/androidx/media/blob/50475814f700d08519c88585c9583f2aba5d702e/libraries/session/src/main/java/androidx/media3/session/SessionToken.java#L326 Note that this list doesn't include sessions that are not associated to a service (MediaSessionService or MediaLibraryService or MediaBrowserServiceCompat).

entibaldi commented 2 years ago

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.

tonihei commented 2 years ago

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?

entibaldi commented 2 years ago

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.