带 Apple Airpods 的 AVAudioRecorder/AVAudioSession [英] AVAudioRecorder/AVAudioSession with Apple Airpods

查看:30
本文介绍了带 Apple Airpods 的 AVAudioRecorder/AVAudioSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这里已经有人问过这个问题了:

I see the question already asked here:

AirPods 无法用作输入源用于录音机应用

我已签入此线程,但没有回复.

I've checked in with this thread but no response.

但是,有谁知道是否/为什么 AVAudioRecorder 可能无法将 AirPods 用作在应用程序中录制音频的输入设备?我可以通过内置麦克风以及其他 BT 设备(Beats、便宜的 BT 扬声器电话等)进行录音,但是在使用 AirPods 时我无法捕获音频.

But, does anyone know if/why AVAudioRecorder might not be able to use the AirPods as an input device for recording audio in an app? I have audio recording working through the builtin mics as well as with other BT devices (Beats, cheapo BT speaker phone thing, etc.) but when working with the AirPods I'm unable to capture the audio.

此外,在准备录制时,我会循环访问可用输入并强制输入为 BT 设备(请参阅下面的代码),在本例中为 AirPods.同样,适用于除 AirPods 之外的所有其他 BT 设备.

In addition when about to record, I'm looping through the available inputs and forcing the input to be the BT device (see code below) in this case the AirPods. Again, works for all other BT devices except the AirPods.

想法?关于我们在这里做错了什么的任何指导都会很棒.这太令人抓狂了.

Thoughts? Any guidance on what we're doing wrong here would be great. This has been maddening.

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 = DanU2019s 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,除非我们准备好录制.

Turns out the issue we were having had to do with the category that was being set. Due to issues we were having with various Bluetooth output devices we set and keep the audio category set to AVAudioSessionCategoryPlayback except when we're ready to record.

根据此堆栈帖子:AVAudioSession:某些蓝牙设备在我的应用程序上无法正常工作

在上面的代码中,我们在准备录制之前将类别切换到 AVAudioSessionCategoryRecord.虽然这适用于内置麦克风和其他蓝牙设备,但不适用于 AirPods.相反,将类别设置为 AVAudioSessionCategoryPlayAndRecord 允许使用 AirPods 进行录音.

In the above code we switch the category over to AVAudioSessionCategoryRecord before we're about to record. While this works for the builtin mics and other bluetooth devices it did not work with AirPods. Instead, setting the category to AVAudioSessionCategoryPlayAndRecord allows recording to work with the AirPods.

然后,我仍然为整个应用程序中的会话维护一个仅播放类别以进行音频播放.仅在即将录制音频时切换到 PlayAndRecord.

I then still maintain a Playback only category for the session throughout the app for audio playback. Only switching to PlayAndRecord when about to record audio.

附带说明:Apple 并未将 AirPods 列为 MFi 设备.https://mfi.apple.com/MFiWeb/getFAQ.action#1-1

As a side note: Apple does not list the AirPods as an MFi device. https://mfi.apple.com/MFiWeb/getFAQ.action#1-1

这篇关于带 Apple Airpods 的 AVAudioRecorder/AVAudioSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆