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

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

Disconnecting Bluetooth device (airpods), fallback to "small speaker" instead of the "loud speaker" #191

Open Pandazaur opened 2 years ago

Pandazaur commented 2 years ago

Hello,

I encounter a problem when I'm testing my app. When I'm starting a audio + video conversation with the Airpods, the sound is correctly broadcast in my ears but if I disconnect my airpods, the audio is redirected to the "small speaker" (the speaker used when you're in a phone call) instead of the "loud speaker" (the big one usually used in video calls)

First test (Working):


Failing test:

My code which manage InCallManager:

private async initIncallManager() {
        await InCallManager.checkRecordPermission()
        InCallManager.start({ media: 'video' })
        InCallManager.setKeepScreenOn(true)

        if (Platform.OS === 'ios') {
            InCallManager.setForceSpeakerphoneOn(!InCallManager.getIsWiredHeadsetPluggedIn())
        }

        DeviceEventEmitter.addListener('WiredHeadset', ({ isPlugged, hasMic }: WiredHeadsetEventData) => {
            if (isPlugged && hasMic) {
                InCallManager.setForceSpeakerphoneOn(false)
            } else if (Platform.OS === 'ios') {
                InCallManager.setForceSpeakerphoneOn(!isPlugged) // Afin de forcer l'utilisation du speaker si on déconnecte des Airpods
            } else if (Platform.OS === 'android') {
                InCallManager.setSpeakerphoneOn(!isPlugged) // Afin de forcer l'utilisation du speaker si on déconnecte des Airpods
            }
        })
    }

The audio is not redirected correctly. I don't think my code changes anything to the audio redirection behavior. Is there a fix, workaround or any information about this case ?

Thank you

wilmxre commented 1 year ago

hey @Pandazaur i have the same problem now as you, did you find a solution for this?

Pandazaur commented 1 year ago

@wilmxre If I remember well, I added this custom code in my AppDelegate.m

#import <WebRTC/RTCAudioSessionConfiguration.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ....

    RTCAudioSessionConfiguration *webRTCConfiguration = [RTCAudioSessionConfiguration webRTCConfiguration];

      webRTCConfiguration.categoryOptions = (
         AVAudioSessionCategoryOptionAllowBluetooth | 
         AVAudioSessionCategoryOptionDefaultToSpeaker
      );

      return YES;
}