AudioManager.setCommunicationDevice to TYPE_BUILTIN_SPEAKER not working

Viewed 77

I am attempting to implement a Loud Speaker / Earpiece switch in Android 12 using the new AudioManager.setCommunicationDevice in place of the deprecated AudioManager.setSpeakerphoneOn.

The result of the set call is true, but the device does not appear to update (both functionally and by checking AudioManager.getCommunicationDevice before and after).

The application is selected as the default Phone package, AudioManager mode is set to AudioManager.MODE_IN_CALL and the manifest includes

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

Below is a code snippet which shows the effect of trying to just set loud speaker during an active call.

    AudioDeviceInfo loudSpeakerAudioDevice = null;
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    audioManager.setMode(AudioManager.MODE_IN_CALL);

    List<AudioDeviceInfo> audioDevices = audioManager.getAvailableCommunicationDevices();
    for (AudioDeviceInfo audioDevice : audioDevices)
    {
        if (audioDevice.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
        {
            loudSpeakerAudioDevice = audioDevice;
        }
    }

    Log.i("Demo", "Current AudioDevice = " + audioManager.getCommunicationDevice().toString());
    if (audioManager.getCommunicationDevice().getType() == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
    {
        Log.i("Demo", "Setting to loudSpeakerAudioDevice = " + loudSpeakerAudioDevice.toString());
        if (audioManager.setCommunicationDevice(loudSpeakerAudioDevice) == false)
        {
            Log.e("Demo", "Failed to set audio device");
        }
        Log.i("Demo", "New AudioDevice = " + audioManager.getCommunicationDevice().toString());
    }

The output of this shows the device ID is the same before and after the set (21).

2022-07-27 10:30:01.465 27907-27907/? I/Demo: Current AudioDevice = android.media.AudioDeviceInfo@21
2022-07-27 10:30:01.501 27907-27907/? I/Demo: Setting to loudSpeakerAudioDevice = android.media.AudioDeviceInfo@22
2022-07-27 10:30:01.518 27907-27907/? I/Demo: New AudioDevice = android.media.AudioDeviceInfo@21
0 Answers
Related