我正在尝试使用AVAudioSession
在蓝牙耳机(在我的情况下为airpods)和电话扬声器之间进行切换。我将会话初始化为:
AVAudioSessionCategoryOptions options = (AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker);
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:&error];
然后我尝试在输出模式之间交替:
-(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];
});
}
问题是当我在连接蓝牙耳机时尝试覆盖扬声器输出时-根本无法切换到扬声器。此功能适用于有线耳机,或者将设备扬声器切换为耳机时:
[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioPortError];
[session setActive: YES error:&sessionError];
关于我在这里做错什么的任何线索吗?
谢谢
答案 0 :(得分:0)
我正在处理的应用程序中存在完全相同的问题,并且仅在iOS 12+和W1 / H1芯片蓝牙设备(AirPods /新Beats Studio等)上才可重现。
我在WWDC上向苹果音频工程师询问了此问题(通过显示我的错误报告),他们确认这是他们正在解决的已知问题。应该在iOS更新中修复。.稍后再出现:)为了加快速度,您可以复制此错误报告:rdar:// 49734534
P.S。对于此问题,没有很好的解决方法,但是有很少的选择: