avaudiorecorderavassetwriteravassetwriterinput

Audio (`AVAssetWriterInput` & `AVAudioRecorder`) Optimal settings


I'm currently building an app with video and audio recording (foreground and background).

There are some reports from my clients that some of the recordings are failing, now I've checked with their logs and it seems that on some devices the encodings settings are not good. I have search all over the web to find a decent source of information on how should I config the recording objects AVAssetWriterInput & AVAudioRecorder. Currently this is my settings for both of the objects

        let recorderSettings: [String: Any] = [
            AVFormatIDKey: kAudioFormatMPEG4AAC,
            AVNumberOfChannelsKey: 2,
            AVSampleRateKey: 44100,
            AVEncoderBitRateKey: 64000,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]

Any help would be appreciated, thanks

BTW this is the error I'm getting

"AssetWrtiting finished with 
Optional(Error Domain=AVFoundationErrorDomain Code=-11861
"Cannot Encode Media" 
UserInfo={NSLocalizedFailureReason=The encoding parameters are not supported.
NSLocalizedDescription=Cannot Encode Media,
NSUnderlyingError=0x2839b7750 {Error Domain=NSOSStatusErrorDomain Code=-12651 "(null)"}})

Solution

  • Just a FYI, if you are stumbling upon this question.

    The outputs of the AVCaptureSession AVCaptureVideoDataOutput & AVCaptureAudioDataOutput has a method to get the recommended settings for recording called

    func recommendedAudioSettingsForAssetWriter(writingTo outputFileType: AVFileType) -> [AnyHashable : Any]?