Skip to content

Commit fe19e9c

Browse files
committed
Should have been included in previous commit (Ensure onClick is always called for media button clicks)
1 parent 9c46851 commit fe19e9c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

android/src/main/java/com/ryanheise/audioservice/AudioService.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public class AudioService extends MediaBrowserServiceCompat implements AudioMana
4747
private static final int NOTIFICATION_ID = 1124;
4848
private static final int REQUEST_CONTENT_INTENT = 1000;
4949
private static final String MEDIA_ROOT_ID = "root";
50+
// See the comment in onMediaButtonEvent to understand how the BYPASS keycodes work.
51+
// We hijack KEYCODE_MUTE and KEYCODE_MEDIA_RECORD since the media session subsystem
52+
// consideres these keycodes relevant to media playback and will pass them on to us.
5053
public static final int KEYCODE_BYPASS_PLAY = KeyEvent.KEYCODE_MUTE;
5154
public static final int KEYCODE_BYPASS_PAUSE = KeyEvent.KEYCODE_MEDIA_RECORD;
5255
public static final int MAX_COMPACT_ACTIONS = 3;
@@ -565,7 +568,7 @@ private void acquireWakeLock() {
565568
@Override
566569
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
567570
if (listener == null) return false;
568-
KeyEvent event = (KeyEvent)mediaButtonEvent.getExtras().get(Intent.EXTRA_KEY_EVENT);
571+
final KeyEvent event = (KeyEvent)mediaButtonEvent.getExtras().get(Intent.EXTRA_KEY_EVENT);
569572
if (event.getAction() == KeyEvent.ACTION_DOWN) {
570573
switch (event.getKeyCode()) {
571574
case KEYCODE_BYPASS_PLAY:
@@ -589,23 +592,30 @@ public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
589592
case KeyEvent.KEYCODE_MEDIA_REWIND:
590593
onRewind();
591594
break;
592-
// The remaining cases are for media button clicks.
593-
// Unfortunately Android reroutes media button clicks to PLAY/PAUSE
594-
// events making them indistinguishable from from play/pause button presses.
595-
// We do our best to distinguish...
595+
// Android unfortunately reroutes media button clicks to
596+
// KEYCODE_MEDIA_PLAY/PAUSE instead of the expected KEYCODE_HEADSETHOOK
597+
// or KEYCODE_MEDIA_PLAY_PAUSE. As a result, we can't genuinely tell if
598+
// onMediaButtonEvent was called because a media button was actually
599+
// pressed or because a PLAY/PAUSE action was pressed instead! To get
600+
// around this, we make PLAY and PAUSE actions use different keycodes:
601+
// KEYCODE_BYPASS_PLAY/PAUSE. Now if we get KEYCODE_MEDIA_PLAY/PUASE
602+
// we know it is actually a media button press.
596603
case KeyEvent.KEYCODE_MEDIA_PLAY:
597-
// If you press the media button while in the pause state, it resumes.
598-
MediaControllerCompat controller = mediaSession.getController();
599-
if (resumeOnClick && controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_PAUSED) {
600-
onPlay();
601-
break;
602-
}
603-
// Otherwise fall through and pass it to onClick
604604
case KeyEvent.KEYCODE_MEDIA_PAUSE:
605605
// These are the "genuine" media button click events
606606
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
607607
case KeyEvent.KEYCODE_HEADSETHOOK:
608-
listener.onClick(mediaControl(event));
608+
MediaControllerCompat controller = mediaSession.getController();
609+
// If you press the media button while in the pause state, we reactivate the media session.
610+
if (resumeOnClick && controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_PAUSED) {
611+
play(new Runnable() {
612+
public void run() {
613+
listener.onClick(mediaControl(event));
614+
}
615+
});
616+
} else {
617+
listener.onClick(mediaControl(event));
618+
}
609619
break;
610620
}
611621
}

0 commit comments

Comments
 (0)