How to redirect sound to speakers in iOS AppRTC example?

I am trying to redirect sound to speakers in the iOS AppRTC example .

I tried:

AVAudioSession* session = [AVAudioSession sharedInstance]; //error handling BOOL success; NSError* error; //set the audioSession category. //Needs to be Record or PlayAndRecord to use audioRouteOverride: success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; if (!success) NSLog(@"AVAudioSession error setting category:%@",error); //set the audioSession override success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); //activate the audio session success = [session setActive:YES error:&error]; if (!success) NSLog(@"AVAudioSession error activating: %@",error); else NSLog(@"audioSession active"); 

There are no errors, but this does not work. How can i fix this?

+7
ios avfoundation audio webrtc apprtcdemo
source share
3 answers

I decided this by decision. Just listen to AVAudioSessionRouteChangeNotification

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil]; 

And using the didSessionRouteChange selector, as shown below:

 - (void)didSessionRouteChange:(NSNotification *)notification { NSDictionary *interuptionDict = notification.userInfo; NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; switch (routeChangeReason) { case AVAudioSessionRouteChangeReasonCategoryChange: { // Set speaker as default route NSError* error; [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; } break; default: break; } } 
+9
source share

I found a solution at the end.

The reason was because you need to set the AVAudioSession category to AVAudioSessionCategoryPlayback . But for some reason, after establishing a webRTC call, it was set to AVAudioSessionCategoryPlayAndRecord . In the end, I decided to add an observer for AVAudioSessionRouteChangeNotification and switch to AVAudioSessionCategoryPlayback every time I found an unwanted category change. Slightly hacked the solution, but worked at the end. You can check here .

0
source share

phuongle's answer is correct. Although, when you enable overriding, it actually cancels the audio output, even when the user connects to the headphones. There is not much point in playing sound through the speaker when the user is using headphones. To do this, use the following code:

 - (void)didSessionRouteChange:(NSNotification *)notification { NSDictionary *interuptionDict = notification.userInfo; const NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; if (routeChangeReason == AVAudioSessionRouteChangeReasonRouteConfigurationChange) { [self enableLoudspeaker]; } } - (void)enableLoudspeaker { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; AVAudioSessionCategoryOptions options = audioSession.categoryOptions; if (options & AVAudioSessionCategoryOptionDefaultToSpeaker) return; options |= AVAudioSessionCategoryOptionDefaultToSpeaker; [audioSession setActive:YES error:nil]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:nil]; } 
0
source share

All Articles