为Android媒体会话添加自定义用户操作

3
我正在尝试向媒体会话中添加自定义用户操作,以便在 Android Auto 操作卡上显示,但似乎无法使其正常工作。我已经查找了相关资料,但没有找到如何明确添加自定义操作的方法。我已经包含了所有相关代码,这是我正在尝试实现的唯一自定义操作的一部分。
我想知道我缺少什么和/或做错了什么。谢谢。
 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");


           }

           ...

      }

 }

你能发布一张当前在播放控件中得到的输出图像吗?你是否在项目的其他地方使用了额外的空间来保留这些插槽? - salminnella
你找到这个问题的解决方案了吗? - Haroun Hajem
1个回答

4
为了添加自定义操作并给出一个PlayBackStateCompat.Builder,您可以按照以下方式添加自定义操作:
mStateBuilder.addCustomAction(
            new PlaybackStateCompat.CustomAction.Builder(
                    CUSTOM_ACTION_REPLAY,
                    getString(R.string.custom_action_replay),
                    R.drawable.ic_replay).build()
    );

然后,您必须添加处理自定义操作的代码,就像您已经做过的那样。 上面的代码应该可以工作,并在Android Auto上正确显示操作,我使用它来显示自定义操作。希望这可以帮助即使我有点晚了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接