前言
记录一下通过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中代码。
参考文章
《
© 版权声明