Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

ios - 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 = 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)
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...