我试图通过模拟耳机音乐控制来控制通用音乐播放(播放/暂停,跳过等)。有没有一种方法可以广播“ACTION_MEDIA_BUTTON”意图?我尝试了使用sendBroadcast()的常规方式,但它没有起作用。
这个可能吗?
我试图通过模拟耳机音乐控制来控制通用音乐播放(播放/暂停,跳过等)。有没有一种方法可以广播“ACTION_MEDIA_BUTTON”意图?我尝试了使用sendBroadcast()的常规方式,但它没有起作用。
这个可能吗?
试试这个
long eventtime = SystemClock.uptimeMillis();
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendOrderedBroadcast(downIntent, null);
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendOrderedBroadcast(upIntent, null);
您可以将不同的KeyEvent.KEYCODE_MEDIA_*分派给AudioManager
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
AudioManager.registerMediaButtonEventReceiver or MediaSession.setMediaButtonReceiver