8

I'm trying to enable the speaker while I am in a call:

final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(true);

I tried to check after the setSpeakerphoneOn() the audiomanager and get that the speaker is still not on by asking isSpeakerphoneOn()

audioManager.isSpeakerphoneOn();

In my log I can see some errors that I can't understand:

E/AudioManager: Don't setBluetoothScoOn-PackageName: com.myapp.app  on = false
E/AudioManager: Don't setSpeakerphoneOn-PackageName: com.myapp.app  on = true

I can't find anything about this error in the forum.

Didn't work on devices: ZTE Z981, Huawei p9, I already tried this, and android.permission.MODIFY_AUDIO_SETTINGS is granted.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
motis10
  • 2,484
  • 1
  • 22
  • 46

4 Answers4

13

Hi read android developer article

https://developer.android.com/reference/android/media/AudioManager.html

explain it clearly

you need to set Permission: MODIFY_AUDIO_SETTINGS in manifest

add this line in manifest

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
2

I found that setMode() (the current mode) before setSpeakerphoneOn() fixes the error on the logcat:

AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE); 
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
motis10
  • 2,484
  • 1
  • 22
  • 46
  • 2
    `MODE_IN_CALL` is reserved to telephony stock app. Prefer `MODE_IN_COMMUNICATION` instead. https://developer.android.com/reference/android/media/AudioManager.html#setMode(int) – Nicolas Perraut Apr 05 '18 at 16:07
  • It doesn't work on huawei V/AudioManager: setMode mode: 3 2019-09-25 19:38:30.577 1023-1560/? W/AudioService: Forbid set MODE_IN_COMMUNICATION when current mode is MODE_IN_CALL from pid=13000, uid=10409 – Vova K. Sep 25 '19 at 16:39
  • 3
    doesn't work on the pixel 3 either after the android 10 update. – Roehit Kadam Dec 30 '19 at 17:45
  • 2
    doesn't work on the pixel 2 either after the android 10 update – Mateen Chaudhry Jan 19 '20 at 12:12
  • 1
    Samsung J4+ with Android 9 requires `MODE_IN_CALL` to get `setSpeakerphoneOn(true)` to turn on the speaker. It doesn't happen with `MODE_IN_COMMUNICATION`. – kbro Jul 24 '20 at 09:00
2

1.setSpeakerphoneOn() is only working on setMode(AudioManager.MDOE_IN_CALL);

John Joe
  • 12,412
  • 16
  • 70
  • 135
0

Only thing that worked for me was setting

audioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

Without AudioManager.MODE_RINGTONE it does not work ..

Thanks to this post

PaBo
  • 13
  • 3
  • What device and OS version you running? For me it not work on Galaxy S20, Android 11 – DzungPV Mar 26 '21 at 02:09
  • 4
    `AudioManager.MODE_*` is not a bitfield and can't be used that way. `AudioManager.MODE_RINGTONE` is 1 (binary `01`) and `AudioManager.MODE_IN_CALL` is 2 (binary `10`), the resulting value is binary `11` which is 3, so your code is the same as calling `audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION)` which is the recommended value for VoIP as per the official documentation. – aberaud Aug 11 '21 at 19:42