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