簡體   English   中英

Callkit 使用藍牙耳機作為音頻輸出

[英]Callkit use bluetooth headset as audio output

我正在使用 Callkit 處理 VOIP
它工作正常,除了音頻輸出源
它總是通過 iPhone 揚聲器輸出音頻

一些這樣的回答說將AvAudioSession Option設置為AVAudioSessionCategoryOptionAllowBluetooth將工作,但仍然失敗
我嘗試將藍牙耳機設置為首選,就像這樣,失敗了

順便問一下,如何通過耳機播放鈴聲?
下面是我的代碼,按照這個討論中的建議,我在撥號后立即配置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:] 強制切換到藍牙,這會導致更嚴重的問題。 就我而言:我在音頻連接后將此強制切換到藍牙,它起作用了,但是當藍牙設備斷開連接時,我的音頻完全不起作用並且應用程序沒有得到任何音頻路由更改回調,甚至在再次連接藍牙設備后也不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM