Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't record on iPad: Error Domain=NSOSStatusErrorDomain Code=1718449215 "(null)"

I am running on iPad iOS 9.3.4 (latest version as of this writing).

I am running this code:

let settings = [
        AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
        AVSampleRateKey: 44100.0,
        AVNumberOfChannelsKey: 2 as NSNumber,
        AVEncoderAudioQualityKey: AVAudioQuality.High.rawValue
    ]


do {
    audioRecorder = try AVAudioRecorder(URL: audioURL, settings: settings)
    audioRecorder.delegate = self
    audioRecorder.record()

} catch let error as NSError{
  print(error.description)
}

I am catching this error:

Error Domain=NSOSStatusErrorDomain Code=1718449215 "(null)"

When I try to use AVAudioRecorder with Objective-C - I am able to record with no problems. The problem seems to only occur with Swift and only on a device - no issue in simulator.

If I switch out kAudioFormatMPEG4AAC with kAudioFormatLinearPCM, I am able to record - but when I try to play back the recording nothing plays - seems like it hasn't recorded well.

Has anyone been able to record with AVAudioRecorder in Swift lately and have the recording play back on a real iPad? I would like to just have that code.

like image 904
etayluz Avatar asked Aug 16 '16 07:08

etayluz


2 Answers

Output file path extension must be in sync with AVFormatIDKey

For .wav

 let recordSettings:[String:Any] = [AVFormatIDKey:kAudioFormatLinearPCM,
                          AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey:320000,
                          AVNumberOfChannelsKey:2,
                          AVSampleRateKey:44100.0 ] as [String : Any]

For .m4a

let recordSettings:[String:Any] = [AVFormatIDKey:kAudioFormatAppleLossless,
                          AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey:320000,
                          AVNumberOfChannelsKey:2,
                          AVSampleRateKey:44100.0 ] as [String : Any]
like image 194
Sreekanth M Avatar answered Nov 14 '22 21:11

Sreekanth M


Looks like I never set the recording session as being active. I wish the error description was better though.

override init() {
    super.init()

    recordingSession = AVAudioSession.sharedInstance()

    do {
      try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
      try recordingSession.setActive(true)
      recordingSession.requestRecordPermission() { (allowed: Bool) -> Void in
        dispatch_async(dispatch_get_main_queue()) {
          if allowed {
            // success
          } else {
            // TBD: Show a message to the user that they need to give permission in settings app to proceed
          }
        }
      }
    } catch {
      // TBD: Show a message to the user that they need to give permission in settings app to proceed
    }
  }
like image 36
etayluz Avatar answered Nov 14 '22 21:11

etayluz