AVAudioRecorder/AVAudioSession 使用 Apple Airpods
AVAudioRecorder/AVAudioSession with Apple Airpods
我看到这里已经提出了问题:
AirPods not working as an input source for Voice Recorder App
我已经检查过这个话题,但没有回应。
但是,有谁知道if/whyAVAudioRecorder 可能无法使用 AirPods 作为输入设备在应用程序中录制音频?我通过内置麦克风以及其他 BT 设备(Beats、便宜的 BT 扬声器 phone 等)进行录音,但在使用 AirPods 时我无法捕获音频。
此外,当要录制时,我循环访问可用的输入并强制输入为 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)
}
}
我认为 AirPods 是 MFI(为 Iphone 制造)配件,这意味着蓝牙通信通过 ExternalAccessory 框架进行https://developer.apple.com/documentation/externalaccessory
这是苹果演示:
https://developer.apple.com/library/content/samplecode/EADemo/Introduction/Intro.html
提示:协议名称必须放在 Info.plist 中的 UISupportedExternalAccessoryProtocols 键
原来我们遇到的问题与设置的类别有关。由于我们在使用各种蓝牙输出设备时遇到的问题,我们将音频类别设置并保持为 AVAudioSessionCategoryPlayback
,除非我们准备好录制。
根据此堆栈 post:AVAudioSession: Some Bluetooth devices are not working properly on my App
在上面的代码中,我们在开始录制之前将类别切换到 AVAudioSessionCategoryRecord
。虽然这适用于内置麦克风和其他蓝牙设备,但不适用于 AirPods。相反,将类别设置为 AVAudioSessionCategoryPlayAndRecord
允许使用 AirPods 进行录音。
然后我仍然为整个应用程序的会话维护一个仅播放类别以进行音频播放。仅在要录制音频时切换到 PlayAndRecord。
附带说明:Apple 并未将 AirPods 列为 MFi 设备。
https://mfi.apple.com/MFiWeb/getFAQ.action#1-1
我看到这里已经提出了问题:
AirPods not working as an input source for Voice Recorder App
我已经检查过这个话题,但没有回应。
但是,有谁知道if/whyAVAudioRecorder 可能无法使用 AirPods 作为输入设备在应用程序中录制音频?我通过内置麦克风以及其他 BT 设备(Beats、便宜的 BT 扬声器 phone 等)进行录音,但在使用 AirPods 时我无法捕获音频。
此外,当要录制时,我循环访问可用的输入并强制输入为 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)
}
}
我认为 AirPods 是 MFI(为 Iphone 制造)配件,这意味着蓝牙通信通过 ExternalAccessory 框架进行https://developer.apple.com/documentation/externalaccessory
这是苹果演示: https://developer.apple.com/library/content/samplecode/EADemo/Introduction/Intro.html
提示:协议名称必须放在 Info.plist 中的 UISupportedExternalAccessoryProtocols 键
原来我们遇到的问题与设置的类别有关。由于我们在使用各种蓝牙输出设备时遇到的问题,我们将音频类别设置并保持为 AVAudioSessionCategoryPlayback
,除非我们准备好录制。
根据此堆栈 post:AVAudioSession: Some Bluetooth devices are not working properly on my App
在上面的代码中,我们在开始录制之前将类别切换到 AVAudioSessionCategoryRecord
。虽然这适用于内置麦克风和其他蓝牙设备,但不适用于 AirPods。相反,将类别设置为 AVAudioSessionCategoryPlayAndRecord
允许使用 AirPods 进行录音。
然后我仍然为整个应用程序的会话维护一个仅播放类别以进行音频播放。仅在要录制音频时切换到 PlayAndRecord。
附带说明:Apple 并未将 AirPods 列为 MFi 设备。 https://mfi.apple.com/MFiWeb/getFAQ.action#1-1