如何在不使用AudioSessionSetProperty的情况下路由到kAudioSessionProperty_OverrideCategoryEnableBluetoothInput

我的iOS6和工作代码将蓝牙设置为输出:

// create and set up the audio session AVAudioSession* audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; [audioSession setActive: YES error: nil]; // set up for bluetooth microphone input UInt32 allowBluetoothInput = 1; OSStatus stat = 0; stat = AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof (allowBluetoothInput), &allowBluetoothInput ); 

从iOS7开始,不推荐使用AudioSessionSetProperty方法。 遵循此主题如何在不使用AudioSessionSetProperty的情况下将音频路由到扬声器? 您可以将输出更改为AVAudioSessionPortOverrideSpeaker或AVAudioSessionPortOverrideNone但此处没有蓝牙选项。

我的实际目标是支持不使用A2DP但使用HFP的蓝牙设备。

那么如何在不使用弃用方法的情况下实现这一目标呢?

扩展我以前的答案和评论 :

您将使用AVAudioSession方法

 - (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError 

category
AVAudioSessionCategoryPlayAndRecord
AVAudioSessionCategoryRecord

options
AVAudioSessionCategoryOptionAllowBluetooth

在你的回复中你说

这是不一样的,因为这将只允许A2DP蓝牙

但根据Apple的文档

AVAudioSessionCategoryOptionAllowBluetooth
允许蓝牙免提设备显示为可用的输入路径。

我明白这意味着蓝牙HFP,我认为是你所追求的。 至于“强制”,Apple并不太热衷于强制/覆盖用户对设备行为体验的OS控制。

可能这在实践中不起作用 – 我无法测试它。 大概你有,但它失败了(你没有在你的问题中指出)。 但是你在这个问题上达到了Apple的文档限制。 如果你真的无法让它工作,我会倾向于使用已弃用的C接口,并准备对iOS8进行更改。

通过参考这个答案 ,我想出了以下内容:

  [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error]; NSArray* routes = [audioSession availableInputs]; for (AVAudioSessionPortDescription* route in routes) { if (route.portType == AVAudioSessionPortBluetoothHFP) { [audioSession setPreferredInput:route error:nil]; } } 

它似乎与旧属性覆盖的工作方式相同,并重定向输入和输出免提设备。