I am trying to move from RemoteController to MediaController for Lollipop but I'm having trouble getting a MediaController instance. In MediaController it says:
A MediaController can be created through MediaSessionManager if you hold the "android.permission.MEDIA_CONTENT_CONTROL" permission or are an enabled notification listener or by getting a MediaSession.Token directly from the session owner.
However when I look at MediaSessionManager there are no methods other than addOnActiveSessionsChangedListener, getActiveSessions, and removeOnActiveSessionsChangedListener these help me none.
I tried creating a token from a MediaSession but that didn't enable my callback to get anything from other media players.
MediaSessionManager.getActiveSessions() gives you a list of MediaController
instances associated with the currently playing music player(s). You can then use MediaController.registerCallback() to register a MediaController.Callback implementation you've created, which has similar callbacks to the deprecated RemoteController.OnClientUpdateListener (or just send media controls directly back to the MediaController
.
Of course, in most cases you want to keep your list of MediaController
s in sync with the system: that is what MediaSessionManager.addOnActiveSessionsChangedListener() is for: it gives you an updated list of MediaController
s with which to use and is called every time the list of players change. As per the documentation, if you are using the permission you can pass null
in for the ComponentName notificationListener
parameter.