I'm trying to allow for a toggle between bluetooth headsets (airpods in my case) and the phone speaker, using AVAudioSession
. I initialize my session as so:
AVAudioSessionCategoryOptions options = (AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker);
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:&error];
Then I try to alternate between output modes as so:
-(void)setIncomingSoundMode:(IncomingSoundMode)incomingSoundMode{
[self removeAudioRouteChangedObserver];
[NNLogger logFromInstance: self message: @"Audio stream setting use speaker" data: @(incomingSoundMode)];
_incomingSoundMode = incomingSoundMode;
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionPortDescription *routePort = session.currentRoute.outputs.firstObject;
NSString *portType = routePort.portType;
NSLog(@"current port type: %@",portType);
NSError *audioPortError = nil;
if(incomingSoundMode == IncomingSoundModeSpeaker){
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioPortError];
[self muteChannel:NO];
} else if(incomingSoundMode == IncomingSoundModeHeadset){
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&audioPortError];
[self muteChannel:NO];
} else if(incomingSoundMode == IncomingSoundModeBluetooth){
[session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&audioPortError];
[self muteChannel:NO];
} else if(incomingSoundMode == IncomingSoundModeSilent){
[self muteChannel:YES];
}
if(audioPortError){
NSLog(@"audioPortError - %@",audioPortError.localizedDescription);
}
NSError *sessionError = nil;
[session setActive: YES error:&sessionError];
if(sessionError){
NSLog(@"sessionError - %@",sessionError.localizedDescription);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addAudioRouteChangedObserver];
});
}
The issue is when I try to override to speaker output while bluetooth headphones are connected - it simply doesn't switch to the speaker. This same functionality works with wired headphones, or when toggling device speaker to headset:
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioPortError];
[session setActive: YES error:&sessionError];
Any clues on what I'm doing wrong here??
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…