Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to check phone audio output?

I was wondering if there was a way in to check where the audio is being played on (internal speaker, headphone jack, mono, stereo, etc.) and Android device?

Thanks.

like image 482
Flynn Avatar asked Sep 01 '25 18:09

Flynn


2 Answers

Yes, using the AudioManager class it's possible for you to check if a wired or wireless headset is currently connected. If it's not, then you can assume that the sounds you emit will be played out by the device's speakers.

These are the methods will help you accomplish what you're trying to do :

  • isWiredHeadsetOn()
  • isSpeakerphoneOn()
  • isBluetoothA2dpOn()

Hope this answers your question !

like image 161
Jean-Philippe Roy Avatar answered Sep 04 '25 06:09

Jean-Philippe Roy


The accepted answer will totally work but they did deprecate those methods in favor of audioManager.getDevices(). Here's an example of how how you could check for a wired headset using the new method.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    if (audioManager != null) {
        for(AudioDeviceInfo deviceInfo : audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)){
            if(deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADPHONES
                    || deviceInfo.getType()==AudioDeviceInfo.TYPE_WIRED_HEADSET){
                return true;
            }
        }
    }

Now keep in mind that getDevices() is only available in API 23+ so there is a gap in coverage and you may need to use the deprecated methods shown in the accepted answer if your minSdk is below 23.

like image 44
kjanderson2 Avatar answered Sep 04 '25 07:09

kjanderson2