2

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.

Packruler
  • 41
  • 1
  • 5

1 Answers1

5

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 MediaControllers in sync with the system: that is what MediaSessionManager.addOnActiveSessionsChangedListener() is for: it gives you an updated list of MediaControllers 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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    I tried: List controllers = mediaSessionManager.getActiveSessions(new ComponentName(getPackageName(), "ListenerService.class")); Also fails if I try to pass null. Error: "java.lang.SecurityException: Missing permission to control media." I have the permission – Packruler Oct 26 '14 at 18:28
  • So you have a NotificationListenerService? It has access to notifications (check Settings->Sound & Notifications->Notification Access)? Also you should use `new ComponentName(getPackageName(), ListenerService.class)` (no quotes) as the string version assumes a fully qualified class name. – ianhanniballake Oct 26 '14 at 18:32
  • Thank you so much! Needed to do ListenerService.class.getName() but its working – Packruler Oct 27 '14 at 03:15
  • Ah, I figured you'd just use the [ComponentName constructor that takes a Class reference](http://developer.android.com/reference/android/content/ComponentName.html#ComponentName(android.content.Context,%20java.lang.Class>)), but if that works, great! – ianhanniballake Oct 27 '14 at 03:19
  • @ianhanniballake, for me one custom music player is there in my launcher app, I tried `mMediaSessionManager.getActiveSessions(null)` to get on going session list, but this `getActiveSessions` is returning empty list after reboot. so I could not play eny music even after clicking play button. Any hop for solving this issue. Thanks in advance ..! – Navas pk Jan 24 '17 at 11:36
  • @Navaspk it is called `getActiveSessions` for a reason - it is only expect to return active sessions, not apps that have long since stopped playback. – ianhanniballake Jan 24 '17 at 13:49
  • you mean after reboot `getActiveSessions()` will not return active session right? Okay, is there any way to get play back ..? – Navas pk Jan 24 '17 at 16:30
  • @Navaspk - per the [Media Session Callbacks docs](https://developer.android.com/guide/topics/media-apps/audio-app/mediasession-callbacks.html), apps are only active after they start playback through when they are stopped. It is only during that time that you'll be able to interact with them via the `MediaSessionManager` APIs. – ianhanniballake Jan 24 '17 at 16:36
  • 1
    Note that the permission mentioned for `addOnActiveSessionsChangedListener` is restricted for system and vendor apps only – Xiv Jul 04 '17 at 12:32