8

My app is not the media player. I just want to be able to control the current media player using the new MediaControllerCompat class. Currently I am using MediaSessionManager but want to upgrade to the new compat classes.

How do I instantiate the MediaControllerCompat class when I don't have a token or MediaSessionCompat to instantiate it with? I am hoping to get some example code.

timothyjc
  • 2,188
  • 3
  • 29
  • 54

2 Answers2

5

I asked Ian Lake, the Google dev who did the video on MediaSessionCompat, who said it is not possible (https://www.youtube.com/watch?v=FBC1FgWe5X4). We still have to use MediaSessionManager.

timothyjc
  • 2,188
  • 3
  • 29
  • 54
1

Actually you can do it like this:

MediaSessionManager mm = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
List<android.media.session.MediaController> controllers = mm.getActiveSessions(null);
for (android.media.session.MediaController controller : controllers) {
    MediaControllerCompat compat = new MediaControllerCompat(context, MediaSessionCompat.Token.fromToken(controller.getSessionToken()));
        
        // or if you wana use more powerful media2 MediaController
        MediaController controller2 = new MediaController.Builder(context)
                .setSessionCompatToken(MediaSessionCompat.Token.fromToken(controller.getSessionToken()))
                .setControllerCallback()
                .build();
wangpan
  • 461
  • 4
  • 5