20 June 2016 |

When building an app that plays music, it is important to be able to pause the music when the headphones are disconnected. You may start this process by trying to listen to Intents such as Intent.ACTION_HEADSET_PLUG and BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED. Marshmallow even has a really nice AudioDeviceCallback which may be helpful once adoption of Marshmallow has gone up. Since this all seems overly complicated, you may just shrug and say, “typical Android”.

Thankfully, handling this scenario is simple thanks to a poorly named Intent called AudioManager.ACTION_AUDIO_BECOMING_NOISY. The system sends this Intent when the device’s external audio has been disconnected, and audio is about to be routed to the device’s internal speakers.

public class HeadphoneReceiver extends BroadcastReceiver {

    public HeadphoneReceiver(Context context) {
        IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
        context.registerReceiver(this, intentFilter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
            // pause music
        }
    }
}

In the onReceive of your BroadcastReceiver you can handle the intent action for AudioManager.ACTION_AUDIO_BECOMING_NOISY and pause your music. And remember, registering a receiver like this statically in your manifest is now discouraged by the Android team.


Categories

Tags