skip to Main Content

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

2

Answers


  1. Try this:

        audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
        audioRecorder.prepareToRecord()
        audioRecorder.record()
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search