@@ -131,12 +131,12 @@ - (void)startObserving
131131- (void )stopObserving
132132{
133133 _hasListeners = FALSE ;
134-
134+
135135 // Fix for https://github.com/react-native-webrtc/react-native-callkeep/issues/406
136136 // We use Objective-C Key Value Coding(KVC) to sync _RTCEventEmitter_ `_listenerCount`.
137137 @try {
138138 [self setValue: @0 forKey: @" _listenerCount" ];
139- }
139+ }
140140 @catch ( NSException *e ){
141141 NSLog (@" [RNCallKeep][stopObserving] exception: %@ " ,e);
142142 NSLog (@" [RNCallKeep][stopObserving] RNCallKeep parent class RTCEventEmitter might have a broken state." );
@@ -189,7 +189,16 @@ + (void)initCallKitProvider {
189189}
190190
191191+ (NSString *) getAudioOutput {
192- return [AVAudioSession sharedInstance ].currentRoute .outputs .count > 0 ? [AVAudioSession sharedInstance ].currentRoute .outputs [0 ].portType : nil ;
192+ @try {
193+ NSArray <AVAudioSessionPortDescription *>* outputs = [AVAudioSession sharedInstance ].currentRoute .outputs ;
194+ if (outputs != nil && outputs.count > 0 ){
195+ return outputs[0 ].portType ;
196+ }
197+ } @catch (NSException * error) {
198+ NSLog (@" getAudioOutput error :%@ " , [error description ]);
199+ }
200+
201+ return nil ;
193202}
194203
195204+ (void )setup : (NSDictionary *)options {
@@ -554,7 +563,7 @@ + (NSMutableArray *) formatAudioInputs: (NSMutableArray *)inputs
554563{
555564 NSMutableArray *newInputs = [NSMutableArray new ];
556565 NSString * selected = [RNCallKeep getSelectedAudioRoute ];
557-
566+
558567 NSMutableDictionary *speakerDict = [[NSMutableDictionary alloc ]init];
559568 [speakerDict setObject: @" Speaker" forKey: @" name" ];
560569 [speakerDict setObject: AVAudioSessionPortBuiltInSpeaker forKey: @" type" ];
@@ -645,13 +654,13 @@ + (NSString *) getSelectedAudioRoute
645654 AVAudioSession* myAudioSession = [AVAudioSession sharedInstance ];
646655 AVAudioSessionRouteDescription *currentRoute = [myAudioSession currentRoute ];
647656 NSArray *selectedOutputs = currentRoute.outputs ;
648-
657+
649658 AVAudioSessionPortDescription *selectedOutput = selectedOutputs[0 ];
650-
659+
651660 if (selectedOutput && [selectedOutput.portType isEqualToString: AVAudioSessionPortBuiltInReceiver]) {
652661 return @" Phone" ;
653662 }
654-
663+
655664 return [RNCallKeep getAudioInputType: selectedOutput.portType];
656665}
657666
@@ -909,7 +918,7 @@ - (void)configureAudioSession
909918
910919 NSUInteger categoryOptions = AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP;
911920 NSString *mode = AVAudioSessionModeDefault;
912-
921+
913922 NSDictionary *settings = [RNCallKeep getSettings ];
914923 if (settings && settings[@" audioSession" ]) {
915924 if (settings[@" audioSession" ][@" categoryOptions" ]) {
@@ -920,7 +929,7 @@ - (void)configureAudioSession
920929 mode = settings[@" audioSession" ][@" mode" ];
921930 }
922931 }
923-
932+
924933 AVAudioSession* audioSession = [AVAudioSession sharedInstance ];
925934 [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord withOptions: categoryOptions error: nil ];
926935
0 commit comments