setPreferredInput WithBlueTooth无法正常工作

时间:2019-01-11 10:16:50

标签: ios bluetooth audiosession

我正在尝试将蓝牙设备设置为audioSession中的preferredinput。

这是我的代码:

if(speaker)
{
    NSLog(@"Audio use speaker");
    CGFloat set = 5.0f;
    AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, set);
    [sessionInstance overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

}else{
    NSLog(@"QCAudio use headset");
    NSError *audioError = nil;
    BOOL hasAvaliableBuleTooth = NO;
    NSArray* b_routePort = @[AVAudioSessionPortBluetoothHFP];
    NSArray* portDescArr = [[AVAudioSession sharedInstance] availableInputs];
    AVAudioSessionPortDescription* desPortDesc;
    for (AVAudioSessionPortDescription* portDesc in portDescArr){
        if ([b_routePort containsObject:portDesc.portType])
        {
            desPortDesc = portDesc;
            hasAvaliableBuleTooth = YES;
        }
    }
    if (hasAvaliableBuleTooth == YES) {
        NSLog(@"1111111111 avsession  desportDesc = %@",desPortDesc);
        BOOL ret =[[AVAudioSession sharedInstance] setPreferredInput:desPortDesc error:&audioError];
        NSLog(@"ret = %d current route = %@",ret, [[AVAudioSession sharedInstance] currentRoute]);
    }else{
        NSLog(@"222222222222 avsession");
        CGFloat set = 1.0f;
        AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, set);
        [sessionInstance overrideOutputAudioPort:AVAudioSessionPortOverrideNone
                                           error:&error];
    }
}

我有这个迹象:

  

2019-01-11 17:59:51.400283 + 0800 yichaoyun [4196:1228652] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:端口h.ear(MDR-EX750BT)的未知选定数据源(类型:BluetoothHFP )

     

2019-01-11 17:59:51.400381 + 0800 yichaoyun [4196:1228652] 1111111111 avsession desportDesc =

     

2019-01-11 17:59:51.410321 + 0800 yichaoyun [4196:1228652] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:端口h.ear(MDR-EX750BT)的未知选定数据源(类型:BluetoothHFP )

     

2019-01-11 17:59:51.410357 + 0800 yichaoyun [4196:1228652] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields:端口h.ear(MDR-EX750BT)的未知选定数据源(类型:BluetoothHFP )

     

2019-01-11 17:59:51.410447 + 0800 yichaoyun [4196:1228652] ret = 1当前路线=“   );   输出=(       ”   )>

它表明连接蓝牙设备是成功的,但是我只能听到一些声音,然后变成静音。只有一个面临相同问题的人,如何解决?

1 个答案:

答案 0 :(得分:1)

我终于找到了正确的答案。 AVAudioSession应该用于收集和记录,这一点非常重要。

然后像这样setCategory

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord 
withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];

设置preferredinput:

[[AVAudioSession sharedInstance] setPreferredInput:_bluetoothPort error:nil];

当您获得这样的当前输入时:

inputs = (
    "< AVAudioSessionPortDescription: 0x28185bf00, type = MicrophoneBuiltIn; name = iPhone \U9ea6\U514b\U98ce; UID = Built-In Microphone; selectedDataSource = \U524d >"
); 

outputs = (
    "< AVAudioSessionPortDescription: 0x28185bfb0, type = BluetoothA2DPOutput; name = h.ear (MDR-EX750BT); UID = 04:5D:4B:4A:4F:31-tacl; selectedDataSource = (null) >"
)>.  

有效!但是我仍然不明白为什么苹果的输入显示首选输入是“ iPhone MicrophoneBuiltIn”,而“蓝牙耳机”有效。这是一个错误吗?