AVAudioRecorder / AVAudioSession与苹果Airpods

我看到这里已经提出的问题:

AirPods不能作为录音机应用程序的input源

我检查了这个线程,但没有回应。

但是,有谁知道是否/为什么AVAudioRecorder可能无法使用AirPods作为input设备在应用程序中录制audio? 我有内置麦克风以及其他BT设备(Beats,便携式BT扬声器电话等)的录音工作,但使用AirPods时,我无法捕获audio。

此外,当要logging时,我正循环使用可用的input,并强制input为BT设备(请参阅下面的代码),在这种情况下为AirPods。 再次,适用于除AirPods以外的所有其他BT设备。

思考? 任何关于我们在这里做错的指导都是很好的。 这一直是疯狂的。

NSError *error; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryRecord withOptions:audioSession.categoryOptions|AVAudioSessionCategoryOptionAllowBluetooth error:&error]; [audioSession setActive:YES error:nil]; NSLog(@"Data sources: %@", [audioSession availableInputs]); // Data sources: ("<AVAudioSessionPortDescription: 0x1706071b0, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>", "<AVAudioSessionPortDescription: 0x170611bd0, type = BluetoothHFP; name = Dan\U2019s AirPods; UID = 50:32:37:E0:90:37-tsco; selectedDataSource = (null)>" for (AVAudioSessionPortDescription *desc in [audioSession availableInputs]){ NSLog(@"Port desc: %@", desc.portType); // Loop: 1) Port desc: MicrophoneBuiltIn // 2) Port desc: BluetoothHFP if (desc.portType == AVAudioSessionPortBluetoothHFP) { NSLog(@"Trying to change preferred input"); NSError *error; BOOL didSet = [audioSession setPreferredInput:desc error:&error]; NSString *didSetString = didSet ? @"True" : @"False"; NSLog(@"Post change preferred input: %@, error: %@", didSetString, error); // Post change preferred input: True, error: (null) } } 

结果是我们不得不处理正在设定的类别。 由于我们在设置各种蓝牙输出设备时遇到的问题, AVAudioSessionCategoryPlayback除非准备好录制,否则将audio类别设置为AVAudioSessionCategoryPlayback

根据这个堆栈文章: AVAudioSession:一些蓝牙设备无法正常工作在我的应用程序

在上面的代码中,我们将类别切换到AVAudioSessionCategoryRecord之前,我们要logging。 虽然这适用于内置麦克风和其他蓝牙设备,但不适用于AirPods。 相反,将类别设置为AVAudioSessionCategoryPlayAndRecord允许logging与AirPods一起使用。

然后,我仍然保持整个应用程序中的会话audio回放的只回放类别。 录制audio时只切换到PlayAndRecord。

作为一个便笺:苹果公司并没有列出AirPods作为一个MFi设备。 https://mfi.apple.com/MFiWeb/getFAQ.action#1-1

我认为AirPods是MFI(Made for Iphone)配件,这意味着蓝牙通信通过ExternalAccessory框架https://developer.apple.com/documentation/externalaccessory

这里是苹果演示: https : //developer.apple.com/library/content/samplecode/EADemo/Introduction/Intro.html

提示:协议名称必须放在UISupportedExternalAccessoryProtocols项的Info.plist中

更多细节: https : //mfi.apple.com/MFiWeb/getFAQ.action