1

I want to know if it is actually possible to react to headphone volume control buttons in an Android application. Whenever I use iPhone/iPod supported headphones, I can use the pause/forward/rewind button with various music apps but NONE of them can react to the volume control.

I've searched a lot and haven't found anything that addresses this issue directly.

I am curious whether this is a limitation on the Android platform or on the apps that I am using. If its not a limitation, how would I capture the volume rocker events from the headphones in my own Android application.

iPhone/iPod seem to be able to react to all headphone events through the 3.5mm jack. It seems to me it should be possible in Android as well.

Danish
  • 3,708
  • 5
  • 29
  • 48

1 Answers1

1

This is 100% an issue relating to the apps themselves. There are plenty of good examples out there to fix this. For instance, you can solve this issue by:

private MPR mediaPlayerReceiver = new MPR();

onCreate() {
   ...
   IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
   // this is nasty, since most apps set it to 9999, so you need to be higher
   mediaFilter.setPriority(999);
   this.registerReceiver(mediaPlayerReceiver, mediaFilter);
   ...
}

MPR  extends BroadcastReceiver {
    public MPR() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            return;
        }
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        try {
            int action = event.getAction();
            switch(action) {
                case KeyEvent.ACTION_DOWN :
                    // handle play/pause here
                    break;
            }
        } catch (Exception e) {
            // something bad happened
        }
        abortBroadcast();
    }
}
Du3
  • 1,424
  • 6
  • 18
  • 36
  • Look here for and working and explained solution: http://stackoverflow.com/questions/10177417/broadcastreceiver-for-action-media-button-not-working/29517224#29517224 – Rappel Apr 08 '15 at 14:23