adb shell控制多媒体

Android 91es.com站长2024年3月16日 am9:40发布3个月前更新
0
导航号,我的单页导航
目录

前言

记录一下通过adb shell 命令进行控制多媒体。这一套都是Android提供的标准,只要多媒体实现了MediaSession.Callback的响应即可。

正文

mMediaSession = new MediaSession(MusicApp.getContext(), TAG);
mMediaSession.setCallback(mediaSessionCallback);


private final MediaSession.Callback mediaSessionCallback = new MediaSession.Callback() {
    @Override
    public boolean onMediaButtonEvent(@NonNull Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent keyevent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            int keyCode = keyevent.getKeyCode();
            int keyAction = keyevent.getAction();
            if (keyAction == KeyEvent.ACTION_UP) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_MEDIA_NEXT:
                        return true;
                    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                        return true;
                    case KeyEvent.KEYCODE_MEDIA_PAUSE:
                        return true;
                    case KeyEvent.KEYCODE_MEDIA_PLAY:
                        return true;
                }
            }
        }
        return super.onMediaButtonEvent(intent);
    }
};

通过adb shell命令最终进入了上面onMediaButtonEvent()中的处理。

adb shell media控制

adb shell media dispatch pause
adb shell media dispatch play
adb shell media dispatch play-pause
adb shell media dispatch fast-forward
adb shell media dispatch rewind
adb shell media dispatch next
adb shell media dispatch previous

物理按键的控制

adb shell input keyevent 87 // next
adb shell input keyevent 88 // previous
adb shell input keyevent 126 // play
adb shell input keyevent 127 // pause

等等,具体值可以看Keyevent.java中代码。

参考文章

  1. Android车载多媒体开发MediaSession框架理解(建议收藏)

版权声明 1、 本站名称 91易搜
2、 本站网址 https://www.91es.com/
3、 本站部分文章来源于网络,仅供学习与参考,如有侵权请留言
4、 本站禁止发布或转载任何违法的相关信息,如有发现请向站长举报
导航号,我的单页导航

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...