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

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

Unable to change from speaker to bluetooth device instead it is routing audio to phone earpiece #223

Open yashecosmob opened 1 year ago

yashecosmob commented 1 year ago

As I am using setAudioRoute to set back to bluetooth and I am not able to switch

await InCallManager.setAudioRoute(‘’,'BLUETOOTH').then((response) => {
})

iOS Native function

RCT_EXPORT_METHOD(setAudioRoute: (NSString *)uuid
                 inputName:(NSString *)inputName
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
#ifdef DEBUG
   NSLog(@"[RNCallKeep][setAudioRoutesetAudioRoute] - inputName: %@", inputName);
#endif
   @try {
       NSError* err = nil;
       AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
       if ([inputName isEqualToString:@"Speaker"]) {
           BOOL isOverrided = [myAudioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&err];
           if(!isOverrided){
               [NSException raise:@"overrideOutputAudioPort failed" format:@"error: %@", err];
           }
           resolve(@"Speaker");
           return;
       }

       NSArray *ports = [RNInCallManager getAudioInputs];
       for (AVAudioSessionPortDescription *port in ports) {
           if ([port.portName isEqualToString:inputName]) {
               BOOL isSetted = [myAudioSession setPreferredInput:(AVAudioSessionPortDescription *)port error:&err];
               if(!isSetted){
                   [NSException raise:@"setPreferredInput failed" format:@"error: %@", err];
               }
               resolve(inputName);
               return;
           }
       }
   }
   @catch ( NSException *e ){
       NSLog(@"[RNCallKeep][setAudioRoute] exception: %@",e);
       reject(@"Failure to set audio route", e, nil);
   }
}

What I tried:

gustavo-amaro commented 1 year ago

Same here. Any updates?

wilmxre commented 9 months ago

@yashecosmob @gustavo-amaro did you find a solution?

anhquangtech commented 7 months ago

In my case outgoing call. I must request bluetooth permission https://github.com/zoontek/react-native-permissions before. Note:

zxar7 commented 6 months ago

@anhquangtech Can you elaborate on how InCallManager.setForceSpeakerphoneOn(true/false) can be used to switch audio. Especially in iOS. I'm trying to connect to bluetooth in iOS and wasn't able to do so with this method. Is there documentation about the same?

wilmxre commented 6 months ago

InCallManager.setForceSpeakerphoneOn(true/false) is just to route the audio to the Speakers, it has no effect for Bluetooth. For Bluetooth, try InCallManager.chooseAudioRoute('BLUETOOTH'). Be aware that for incoming call, the call has to connect and only after you can route the audio to Bluetooth. So either way try calling the routing method with a button press after the call connection was already established, to test it, or just call it with a setTimeout of a few seconds.

zxar7 commented 6 months ago

@wilmxre Does this mean that it needs to be set a few seconds after InCallManager.start()? Also InCallManager.chooseAudioRoute('BLUETOOTH') just returns the error TypeError: undefined is not a function. I'm not sure it works anymore.

zxar7 commented 6 months ago

Oddly enough in Android the InCallManager.start() alone seems to route it correctly between the phone's mic and bluetooth. But the same does not work for iOS.

katiahr commented 2 months ago

InCallManager.chooseAudioRoute('BLUETOOTH') not working