【问题标题】:Callkit use bluetooth headset as audio outputCallkit 使用蓝牙耳机作为音频输出
【发布时间】:2017-12-11 13:30:35
【问题描述】:

我正在使用 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];

}

【问题讨论】:

  • 嘿@xxi 我知道这个问题有点老了,但看起来我遇到了同样的问题。有这方面的消息吗?你有没有设法解决它?
  • 抱歉,不走运。我放弃了蓝牙功能:(

标签: ios audio bluetooth voip callkit


【解决方案1】:

经过研究,我找到了这个问题的根源。 打开设置->辅助功能->触控->呼叫音频路由,共有三个选项:自动、蓝牙耳机、扬声器。 默认为自动,这意味着如果您接听电话(包括 callkit),当您通过蓝牙设备接听电话时,音频将路由到蓝牙,如果您在 iPhone 中接听电话,它将路由到接收器。 所以这实际上不是问题,只是系统行为。

并且不要尝试使用 [AVAudioSession setPreferredInput:] 强制切换到蓝牙,这会导致更严重的问题。 在我的情况下:我在音频连接后将此强制开关称为蓝牙,它可以工作,但是当蓝牙设备断开连接时,我的音频完全无法工作,应用程序没有收到任何音频路由更改回调,甚至在再次连接蓝牙设备后也无法工作。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多