与Apple Airpods一起使用的AVAudioRecorder/AVAudioSession [英] AVAudioRecorder/AVAudioSession with Apple Airpods

查看:127
本文介绍了与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设备(节拍器,cheapo 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之外的所有其他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 = 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,除非准备进行录制.

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.

根据此Stack帖子:

Per this Stack post: AVAudioSession: Some Bluetooth devices are not working properly on my App

在上面的代码中,我们将在记录之前将类别切换到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天全站免登陆