'audioManager not working on Android 11 and 12

I am making an internal calls application, when I call a number, the native android dialer opens and then instead of following the call in the native dialer, I follow the call in the app. The problem is that from the app it has no effect when I want to activate or deactivate the native dialer speaker. The native dialer is in the background, and the app is in the foreground, but for some reason when I try to activate the speaker from the app, to follow the call from here, it doesn't work. This only happens in versions of Android 11 and 12, however in versions of Android 8,9 and 10 it works perfectly, I can manipulate the speaker from the app, without going to the native dialer.

This is my code to activate and desactivate the speaker:

private fun enableSpeaker() {
        audioManager?.let {
            if (!it.isSpeakerphoneOn) {
                audioManager!!.mode = AudioManager.MODE_IN_COMMUNICATION
                audioManager!!.isSpeakerphoneOn = true
            }
        }
    }
private fun disableSpeaker() {
        audioManager?.let {
            if (it.isSpeakerphoneOn) {
                //audioManager.setMicrophoneMute(false)
                audioManager!!.mode = AudioManager.MODE_NORMAL
                audioManager!!.isSpeakerphoneOn = false
            }
        }
    }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source