react-native-webrtc / react-native-incall-manager

Handling media-routes/sensors/events during a audio/video chat on React Native
ISC License
555 stars 192 forks source link

setSpeakerphoneOn does not work on android 14 #242

Open KolissnikBogdan opened 6 months ago

KolissnikBogdan commented 6 months ago

Hello everyone, after updating my phone to android 14, this function stopped working: setSpeakerphoneOn(..) I will be very grateful for any information regarding this problem.

KolissnikBogdan commented 4 months ago

my current solution:

Снимок экрана 2024-06-26 в 17 02 21
xKRUTOIx commented 1 month ago

I made a patch that works for me.

Remove this:

https://github.com/react-native-webrtc/react-native-incall-manager/blob/626813946eb2a3abc2a2bdfca44085c999861327/android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java#L862-L869

Add this:

@ReactMethod
    public void setSpeakerphoneOn(final boolean enable) {
        // Request audio focus to ensure the app can control audio output
        requestAudioFocus();

        // Set the appropriate audio mode based on whether the speakerphone is being enabled or disabled
        audioManager.setMode(enable ? AudioManager.MODE_IN_COMMUNICATION : AudioManager.MODE_NORMAL);

        // Check if the device is running Android 12 (API level 31) or higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Android 12 and above
            if (enable) {
                // Retrieve a list of all available output audio devices
                AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);

                // Iterate through the list to find the built-in speaker
                for (AudioDeviceInfo device : devices) {
                    if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
                        // Set the communication device to the built-in speaker
                        audioManager.setCommunicationDevice(device);
                        break; // Exit the loop once the speaker is set
                    }
                }
            } else {
                // Clear the communication device to revert to the default audio routing
                audioManager.clearCommunicationDevice();
            }
        } else {
            // For Android versions below API level 31, use the traditional method to toggle the speakerphone
            audioManager.setSpeakerphoneOn(enable);
        }
    }