我正在使用Callkit进行VOIP它工作正常,但音频输出源除外它总是通过iPhone扬声器输出音频
有些回答说将AvAudioSession Option
设置为AVAudioSessionCategoryOptionAllowBluetooth
即可,但仍然失败并且我尝试将蓝牙耳机设置为首选,例如this,失败
顺便问一下,如何通过耳机播放铃声?下面是我的代码,请按照此discussion中的建议进行操作,我在拨号后立即配置AVAudioSession
并接听电话
- (void)getCall:(NSDictionary *)infoDic {
CXCallUpdate *update = [[CXCallUpdate alloc]init];
// config update
NSUUID *uuid = [NSUUID UUID];
[self.provider reportNewIncomingCallWithUUID:uuid update:update completion:^(NSError * _Nullable error) {
if (error)
NSLog(@"%@", error.localizedDescription);
}];
NSArray *video = @[@(ReceiveVideoReq), @(VideoCalling)];
if ([video containsObject:@(self.client.callStage)])
[ProviderManager configureAudio:true];
else
[ProviderManager configureAudio:false];
}
- (void)dialPhone:(BOOL)isVideo {
CXHandle *handle = [[CXHandle alloc]initWithType:CXHandleTypePhoneNumber value:@"AAAA"];
CXStartCallAction *start = [[CXStartCallAction alloc]initWithCallUUID:uuid handle:handle];
start.video = isVideo;
CXTransaction *trans = [[CXTransaction alloc]initWithAction:start];
[self callControlReq:trans];
[ProviderManager configureAudio:isVideo];
}
+ (void)configureAudio:(BOOL)isVideo {
NSError *error=nil, *sessionError = nil;
AVAudioSession *sess = [AVAudioSession sharedInstance];
[sess setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP error:&sessionError];
if (sessionError)
NSLog(@"ERROR: setCategory %@", [sessionError localizedDescription]);
if (isVideo)
[sess setMode:@"AVAudioSessionModeVideoChat" error:&sessionError];
else
[sess setMode:@"AVAudioSessionModeVoiceChat" error:&sessionError];
if (sessionError) {
NSLog(@"ERROR: setCategory %@", [sessionError localizedDescription]);
}
[sess overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&sessionError];
[[AVAudioSession sharedInstance] setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
经过研究,我找到了这个问题的根本原因。打开设置->辅助功能->触摸->呼叫音频路由,共有三个选项:自动,蓝牙耳机,扬声器。默认为自动,这意味着如果您接听电话(包括callkit),则当您通过蓝牙设备接听电话时,音频将路由至蓝牙,如果您在iPhone中接听电话,则将路由至接收方。因此,这实际上不是问题,而只是系统行为。
并且不要尝试通过使用[AVAudioSession setPreferredInput:]强制切换到蓝牙,这将导致更严重的问题。就我而言:在音频连接后,我将此强制开关切换为蓝牙,它起作用了,但是当蓝牙设备断开连接时,我的音频完全不起作用,并且应用程序没有得到任何音频路由更改回调,甚至在再次连接蓝牙设备后也无法工作。] >