简体   繁体   English

setPreferredInput WithBlueTooth无法正常工作

[英]setPreferredInput WithBlueTooth not working

I'm trying to set bluetooth device into preferinput in audioSession. 我正在尝试将蓝牙设备设置为audioSession中的preferredinput。

Here is my code: 这是我的代码:

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];
    }
}

and I've got this sign: 我有这个迹象:

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.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.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.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: 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:端口h.ear(MDR-EX750BT)的未知选定数据源(类型:BluetoothHFP)

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

It shows that connect bluetooth device is success.but I can only hear some nosie.then it turned to silence.Is there only one who face the same question?How can I fix it? 它表明连接蓝牙设备是成功的,但是我只能听到一些声音,然后变成静音。是否只有一个面临相同问题的人,我该如何解决?

I finally found the right answer. 我终于找到了正确的答案。 AVAudioSession should be used to collect and record which is very important. 应使用AVAudioSession进行收集和记录,这一点非常重要。

And then setCategory like this: 然后像这样设置setCategory

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

Set preferinput: 设置preferredinput:

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

When you got current input like this: 当您获得这样的当前输入时:

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

It works! 有用! But I still don't understand why Apple's input shows that the prefer inputs is 'iPhone MicrophoneBuiltIn' but 'Bluetooth headphones' works.Is it a bug? 但是我仍然不明白为什么苹果的输入显示首选输入是``iPhone MicrophoneBuiltIn''而``蓝牙耳机''有效,这是一个错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM