Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headphone Jack Listener Android

Does anyone know how I can detect if the headphone jack on a device is unplugged on Android? I have a music player and I need to pause the music when the headphones are unplugged. The closest thing I have found is using the AudioManager. Is that the right direction to go?

like image 864
DRiFTy Avatar asked Apr 02 '12 23:04

DRiFTy


1 Answers

This is what I ended up doing:

private class NoisyAudioStreamReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
            pause();
        }
    }
}

private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);

private void startPlayback() {
    registerReceiver(myNoisyAudioStreamReceiver(), intentFilter);
}

private void stopPlayback() {
    unregisterReceiver(myNoisyAudioStreamReceiver);
}

I found the answer at this link: http://developer.android.com/training/managing-audio/audio-output.html

like image 98
DRiFTy Avatar answered Sep 21 '22 18:09

DRiFTy