2

I am trying to get a voice recorder to capture input from the the AirPods Pro. The following function performs the audio capture, which works well with the builtinmic but does not work with the AirPods. The issue persists in both the simulator and on a real device. Could someone point out on what I am missing here to enable bluetooth capture? Thanks!

func startRecording() {
        let recordingSession = AVAudioSession.sharedInstance()
        
        // Then we start recording session with
        do {
            try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker, .allowBluetoothA2DP])
            try recordingSession.setActive(true)
        } catch {
            print("Failed to set up recording session")
            
        }
        
        // Then we say where to save it
        let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        
        // file is named after the date and time of the recording in the .m4a format.
        let audioFilename = documentPath.appendingPathComponent("\(Date().toString( dateFormat: "dd-MM-YY_'at'_HH:mm:ss")).m4a")
        
        let settings = [
                    AVFormatIDKey: Int(kAudioFormatLinearPCM),
                    AVSampleRateKey: 48000,
                    AVNumberOfChannelsKey: 1,
                    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
                ]
        
        do {
            audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
            audioRecorder.record()
            recording = true
        } catch {
            print("Could not start recording")
        }
    } //end func startRecording
jnpdx
  • 45,847
  • 6
  • 64
  • 94
gadha007
  • 31
  • 1
  • 5
  • any help please? – gadha007 May 08 '21 at 21:11
  • Hello! I know that this is not accepted. But for a long time I cannot resolve the issue. Could you help me? I would be very grateful. I can't find AirPods Pro in my area. Could you just open up my small xcode project and test it please? https://stackoverflow.com/questions/69851479/audio-files-wont-play-with-airpods-pro-on-ios-15 –  Nov 13 '21 at 19:04

2 Answers2

3

Your problem is in your options: [.allowBluetooth, .defaultToSpeaker, .allowBluetoothA2DP]. A2DP does not support recording, so when you put the device in A2DP mode, its microphone is disabled. You need to remove the .allowBluetoothA2DP option

Note that when you do this, it'll switch to HFP mode (which is what .allowBluetooth actually means). This will significantly degrade audio output. For a voice recorder that's probably ok, but if you're trying to record from the AirPod's microphone while playing high-quality audio, that's not supported.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

Try this:

    audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
    audioRecorder.prepareToRecord()
    audioRecorder.record()
cbiggin
  • 1,942
  • 1
  • 17
  • 18
  • 1
    Can you discuss how this will enable the AirPod's microphone? Note `prepareToRecord` is called automatically by `record`. There should never be a reason to call it immediately before `record`. It's only useful to call ahead of time, if you know you're going to record later and want recording to start more quickly then by doing the initialization now. – Rob Napier Jul 20 '21 at 20:37
  • So I did already have the settings suggested by @Rob Napier in my code but I think that SwiftUI is just buggy and was until the newest updates and now things seem to work as expected. Thanks for the suggestions! – gadha007 Sep 24 '21 at 23:22