Android set speakerphone on programmatically

16.6k views Asked by At

I am trying to set speakerphone on in a call programmatically using audiomanager.setSpeakerphoneOn(true) in a service but it seems it is not working and I don't know why. Should I do something else?

Thanks

2

There are 2 answers

0
the100rabh On BEST ANSWER

check if your manifest file has the permissions need to do this operation.

I think this is the permission you need MODIFY_AUDIO_SETTINGS

2
Eran Katsav On

In android 4.1 and more when you make a call the phone turn off the speaker phone automatically. So What you need to do is to add the speakerphone on in the reciever that listens for the call being made when the state is "offHook" and even put a 0.5 second delayed to turn on the speaker like that:

final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    MainActivity.shouldTurnSpeakerOn = false;
    MainActivity.shouldTurnSpeakerOff = true;
    Log.d("incoming_call","speaker_on");                    
    }
}, 500);

Remember to add this to the Phone state listener. And to run off the speaker phone when the state is IDLE (needed for earlier versions).

Good Luck.