Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioRecorder/AVAudioSession with Apple Airpods

I see the question already asked here:

AirPods not working as an input source for Voice Recorder App

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

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.

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)
    }
}
like image 840
djneely Avatar asked Oct 06 '17 18:10

djneely


People also ask

Can you use AirPods with AirPlay?

How to start sharing. Connect your AirPods or AirPods Pro, your AirPods Max, or your Beats headphones to your iOS or iPadOS device. Tap the AirPlay button in Control Center on your iPhone or iPad, on the Lock Screen, or in the app that you're listening to. Tap Share Audio.


1 Answers

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.

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

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.

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.

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

like image 91
djneely Avatar answered Sep 20 '22 14:09

djneely