Android set speakerphone on programmatically
Asked Answered
W

2

15

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

Wingate answered 18/4, 2010 at 17:5 Comment(1)
A similar question was asked and answered [here][1] [1]: #8617881Kristin
V
18

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

I think this is the permission you need MODIFY_AUDIO_SETTINGS

Variola answered 18/4, 2010 at 18:38 Comment(0)
M
4

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.

Mansuetude answered 17/8, 2014 at 6:20 Comment(2)
I don't know the reason why but this delay of 500 ms is required. Thanks!Toritorie
not working in android 10 pixel 2 do you know the reason?Cotterell

© 2022 - 2024 — McMap. All rights reserved.