I want to use the new MediaController
in Android 5 instead of the old RemoteController
for getting the currently playing track and changing the tra
UPDATE: A comment clarified that the question was actually about viewing/controlling other apps' MediaSessions, not your own.
Although there is no way to do it directly, due to privacy reasons, you have two options with different levels of available controls/information and different requirements for user interaction:
If you just want to skip musics or play/pause, you can send media button events (via AudioManager) and/or request/release audio focus.
If you need to also know the metadata of what is currently playing, there is a more intrusive way that requires explicit user interaction:
First, create and register a NotificationListenerService:
public class NotificationListener extends NotificationListenerService {
public NotificationListener() {
}
}
In AndroidManifest.xml:
Then you will be able to fetch MediaSessions by specifying your NotificationListenerService componentName in the getActiveSessions call:
MediaSessionManager mm = (MediaSessionManager) this.getSystemService(
Context.MEDIA_SESSION_SERVICE);
List controllers = mm.getActiveSessions(
new ComponentName(this, NotificationListener.class));
Log.i(TAG, "found " + controllers.size() + " controllers");
One caveat is that the user will need to explicitly give your app Notification access permission, by going to Settings -> Sound & Notification -> Notification access
The object that describes your MediaSession and that can be passed along to allow other components/apps to control an existing MediaSesion is the MediaSession.Token. With a token, you can create a MediaController directly, without resorting to the MediaSessionManager. The code for that would be something like:
MediaController mediaController = new MediaController(getActivity(),
sessionToken);
This doesn't require any special permission. If you are also using the MediaBrowser or the MediaBrowserService, you should get the token associated with the MediaBrowser, by using it's getSessionToken() method.
We just released an example that uses a MediaBrowserService to handle music browsing, playback and media style notification and provides a simple Activity to control playback.