@@ -22,6 +22,7 @@ @implementation RNAudioManager
2222
2323static RNAudioManager *sharedInstance = nil ;
2424BOOL *speakerEnabled;
25+ AVAudioSessionMode sessionMode;
2526
2627#pragma mark Singleton Methods
2728+ (id )sharedInstance {
@@ -30,15 +31,11 @@ + (id)sharedInstance {
3031 dispatch_once (&onceToken, ^{
3132 sharedInstance = [self alloc ];
3233 });
33- // [sharedInstance configureAudioSession];
3434 [sharedInstance listenForAudioRoutesChanges ];
3535 [sharedInstance listenForInterruptions ];
3636 [sharedInstance listenForMediaServicesReset ];
3737 [sharedInstance listenForMediaServicesWereLost ];
38-
39- if (@available (iOS 14.5 , *)) {
40- [[AVAudioSession sharedInstance ] setPrefersNoInterruptionsFromSystemAlerts: NO error: nil ];
41- }
38+ sessionMode = AVAudioSessionModeVoiceChat;
4239 }
4340 return sharedInstance;
4441}
@@ -89,18 +86,14 @@ - (void)configureAudioSession {
8986 AVAudioSession *session = [AVAudioSession sharedInstance ];
9087 NSError *error;
9188
92- if (![session setActive: NO error: &error]) {
93- NSLog (@" [IIMobile - RNAudioManager][configureAudioSession] inactivate session failed with error %@ " , [error debugDescription ]);
94- }
95-
9689 if (![session setCategory: AVAudioSessionCategoryPlayAndRecord
9790 withOptions: (AVAudioSessionCategoryOptionAllowBluetooth, AVAudioSessionCategoryOptionAllowBluetoothA2DP)
9891 error: &error
9992 ]) {
10093 NSLog (@" [IIMobile - RNAudioManager][configureAudioSession] setCategory failed with error %@ " , [error debugDescription ]);
10194 }
10295
103- if (![session setMode: AVAudioSessionModeVoiceChat error: &error]) {
96+ if (![session setMode: sessionMode error: &error]) {
10497 NSLog (@" [IIMobile - RNAudioManager][configureAudioSession] setMode failed with error %@ " , [error debugDescription ]);
10598 }
10699
@@ -136,10 +129,6 @@ - (void)handleAudioRouteChange: (NSNotification *) notification {
136129 NSLog (@" [IIMobile - RNAudioManager][handleRouteChange] with reason %@ from OUTPUT %@ to %@ " , reason, previousOutput.portType , output.portType );
137130
138131 if (input != nil ) {
139- if (![input.portType isEqualToString: AVAudioSessionPortBuiltInMic]) {
140- [self toggleAudioRoute: false ];
141- }
142-
143132 if (!([input.portType isEqualToString: AVAudioSessionPortBuiltInMic] && speakerEnabled)) {
144133 [RNEventEmitterHelper emitEventWithName: @" audioRouteChanged"
145134 andPayload: @{
@@ -149,16 +138,6 @@ - (void)handleAudioRouteChange: (NSNotification *) notification {
149138 }
150139 } else {
151140 NSLog (@" [IIMobile - RNAudioManager][handleRouteChange] cannot change audio route: input cannot be null" );
152- NSError *error;
153- [session setPreferredInput: previousInput error: &error];
154- if (error != nil ) {
155- NSLog (@" [IIMobile - RNAudioManager][handleAudioRouteChange] an error ocurred while setPreferredInput: %@ " , [error debugDescription ]);
156- }
157- error = nil ;
158- [session setActive: YES error: &error];
159- if (error != nil ) {
160- NSLog (@" [IIMobile - RNAudioManager][handleAudioRouteChange] an error ocurred while activating the audio session: %@ " , [error debugDescription ]);
161- }
162141 }
163142}
164143
@@ -169,13 +148,7 @@ - (void)handleInterruptions: (NSNotification *) notification {
169148
170149- (void )handleMediaServerReset : (NSNotification *) notification {
171150 NSLog (@" [IIMobile - RNAudioManager][handleMediaServerReset]" );
172- NSError *error;
173-
174151 [self configureAudioSession ];
175-
176- if (![[AVAudioSession sharedInstance ] setPreferredInput: nil error: &error]) {
177- NSLog (@" [IIMobile - RNAudioManager][handleMediaServerReset] clear preferred input failed with error: %@ " , [error debugDescription ]);
178- }
179152}
180153
181154- (void )handleMediaServerServicesWereLost : (NSNotification *) notification {
@@ -275,16 +248,20 @@ - (void)toggleAudioRoute:(BOOL *)toSpeaker {
275248 if (portType == nil ) {
276249 reject (@" error" , @" portName is required" , nil );
277250 }
278- BOOL speakerEnabled = [portType isEqualToString: AVAudioSessionPortBuiltInSpeaker];
279- [self toggleAudioRoute: &speakerEnabled ];
251+ BOOL enableSpeaker = [portType isEqualToString: AVAudioSessionPortBuiltInSpeaker];
252+ [self toggleAudioRoute: &enableSpeaker ];
280253
281- if (speakerEnabled ) {
254+ if (enableSpeaker ) {
282255 resolve (portType);
283256 } else {
284257 AVAudioSession* session = [AVAudioSession sharedInstance ];
285258 AVAudioSessionPortDescription* newPort = [self getAudioDeviceFromType: portType];
286259 NSError * error;
287260
261+ if (speakerEnabled) {
262+ [self toggleAudioRoute: FALSE ];
263+ }
264+
288265 [session setPreferredInput: newPort error: &error];
289266 if (error != nil ) {
290267 NSLog (@" [IIMobile - RNAudioManager][setPreferredInput] failed with error %@ " , [error debugDescription ]);
@@ -296,6 +273,16 @@ - (void)toggleAudioRoute:(BOOL *)toSpeaker {
296273 }
297274}
298275
276+ RCT_EXPORT_METHOD (configure: (NSString *) mode) {
277+ if ([mode isEqualToString: @" video" ]) {
278+ sessionMode = AVAudioSessionModeVideoChat;
279+ } else {
280+ sessionMode = AVAudioSessionModeVoiceChat;
281+ }
282+
283+ [self configureAudioSession ];
284+ }
285+
299286+ (BOOL )requiresMainQueueSetup {
300287 return YES ;
301288}
0 commit comments