I'm trying to set bluetooth device into preferinput in audioSession.
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.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 = ( "" )>
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.
And then setCategory
like this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];
Set preferinput:
[[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?