Suhayl Hallim
Suhayl Hallim

Reputation: 31

Adding a Custom User Action to the Android Media Session

I'm trying to add a custom user action to my media session so that it shows up on the android auto action card, but I can't seem to get it to work. I've looked around, but didn't find much on how to explicitly add a custom action. I've included all the relevant code that is a part of my implementation of the only custom action I am trying to implement.

I would like to know what is it I'm missing and/or is doing wrong. Thank you.

 public class MyMediaBrowserService extends MediaBrowserServiceCompat{

      //Variables Declared here ...

      @Override
      public void onCreate(){

           mediaSession = new MediaSessionCompat(this, TAG);
           mediaSessionCallback = new MyMediaSessionCallback();
           mediaSession.setCallback(mediaSessionCallback);

           // Enable callbacks from MediaButtons and TransportControls
           mediaSession.setFlags(
                MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

           MediaSessionCompat.Token token = mediaSession.getSessionToken();
           setSessionToken(token);

           mediaNotificationManager = new MediaNotificationManager(this);

           // Set an initial PlaybackState with ACTION_PLAY, so media 
                buttons can start the player
           mStateBuilder = new PlaybackStateCompat.Builder()
                .setActions(
                    PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .addCustomAction(CUSTOM_ACTION_REPEAT, "Repeat Mode", 
                     R.drawable.ic_repeat_none);

           mediaSession.setPlaybackState(mStateBuilder.build());

           mediaSession.setActive(true);
           mediaPlayer = new DefaultMediaPlayer(this, 
                                       new DefaultMediaPlaybackListener());

      }

      ...

      public class MyMediaSessionCallback extends 
           MediaSessionCompat.Callback {

           //Variables Declared here ...

           // The following methods are actually implemented in the 
           //  project, and functions as they are supposed to.
           // They are mentioned here for the sake of showing their 
           //  existence with relations to the PlaybackStateCompat set 
           //  above.
           @Override public void onAddQueueItem(MediaDescriptionCompat 
                                                description) { ... }
           @Override public void onRemoveQueueItem(MediaDescriptionCompat 
                                                   description) { ... }
           @Override public void onPlayFromMediaId(String mediaId, 
                                                   Bundle extras) { .... }
           @Override public void onPlay() { ... }
           @Override public void onPause() { ... }
           @Override public void onStop() { ... }
           @Override public void onSkipToNext() { ... }
           @Override public void onSkipToPrevious() { ... }
           @Override public void onSeekTo(long pos) { ... }

           // This is the actual implement of the onCustomAction method
           // I never got it working so I figured I'll start by logging it 
           //  first before spending time coding it 
           @Override
           public void onCustomAction(String action, 
                                      Bundle extras) {

                if(action.equals(CUSTOM_ACTION_REPEAT))
                     Log.e(TAG, "Custom action is REPEAT");


           }

           ...

      }

 }

Upvotes: 3

Views: 4015

Answers (1)

Armaggheddon
Armaggheddon

Reputation: 348

In order to add a custom action and given a PlayBackStateCompat.Builder you can add a custom action as follows:

mStateBuilder.addCustomAction(
            new PlaybackStateCompat.CustomAction.Builder(
                    CUSTOM_ACTION_REPLAY,
                    getString(R.string.custom_action_replay),
                    R.drawable.ic_replay).build()
    );

Then you must add the code to handle the custom action as you already did. The code above should work, and correctly display the actions on Android Auto, i use this to display the custom action. Hope this helps even if i am little late

Upvotes: 4

Related Questions