AVAudioRecorder does not throw error but fails to record audio for some settings

I am trying to record audio using AVAudioRecorder.


If I try to record with the following configuration, I find that the recording is successful,


let settings: [String: Any] = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVSampleRateKey: 22050,
    AVEncoderBitRateKey: 128000,
    AVNumberOfChannelsKey: 2,
    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
        
try audioRecorder = AVAudioRecorder(url: audioFileURL, settings: settings)
audioRecorder?.delegate = self


If I try to record with after changing the bit rate, I find that the recording is not successful.

let settings: [String: Any] = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVSampleRateKey: 22050,
    AVEncoderBitRateKey: 160000,
    AVNumberOfChannelsKey: 2,
    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
        
try audioRecorder = AVAudioRecorder(url: audioFileURL, settings: settings)
audioRecorder?.delegate = self


In the debug console, the following message is shown when `audioRecorder.record()` is called but no errors are thrown

2125:  AudioCodecInitialize failed


Can anyone point how to detect this condition as errors are not thrown?


Edit - I have implemented the delegate method func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) so shouldn't this method be called if audioRecorder failed for any reason.

Replies

Same issue here... did you ever find a resolution?


I've run example projects with audio recording without a problem, so I am really beating my head against the wall.