setPreferredInput WithBlueTooth 不工作

setPreferredInput WithBlueTooth not working

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

这是我的代码:

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: Unknown selected data source for Port h.ear (MDR-EX750BT) (type: 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: Unknown selected data source for Port h.ear (MDR-EX750BT) (type: BluetoothHFP)

2019-01-11 17:59:51.410357+0800 yichaoyun[4196:1228652] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port h.ear (MDR-EX750BT) (type: BluetoothHFP)

2019-01-11 17:59:51.410447+0800 yichaoyun[4196:1228652] ret = 1 current route = " ); outputs = ( "" )>

显示连接蓝牙设备是success.but我只能听到一些nosie.then它转向silence.Is只有一个人面临同样的问题?我该如何解决?

我终于找到了正确答案。 AVAudioSession应该用来收集和记录这很重要。

然后 setCategory 这样:

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

设置首选输入:

[[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) >"
)>.  

有效!但是我还是不明白为什么Apple的输入显示首选输入是'iPhone MicrophoneBuiltIn'但是'Bluetooth headphones'works.Is这是一个错误?