无法在 iPad 上录制:Error Domain=NSOSStatusErrorDomain Code=1718449215 "(null)"

eta*_*luz 5 avaudiorecorder ipad ios

我在 iPad iOS 9.3.4(撰写本文时的最新版本)上运行。

我正在运行此代码:

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)
}
Run Code Online (Sandbox Code Playgroud)

我发现了这个错误:

错误域=NSOSStatusErrorDomain 代码=1718449215“(空)”

当我尝试将 AVAudioRecorder 与 Objective-C 一起使用时 - 我能够毫无问题地进行录制。问题似乎只出现在 Swift 上,而且只出现在设备上 - 在模拟器中没有问题。

如果我用 kAudioFormatLinearPCM 切换 kAudioFormatMPEG4AAC,我可以录制 - 但是当我尝试播放录音时,没有播放 - 似乎录制效果不佳。

最近有没有人能够在 Swift 中使用 AVAudioRecorder 进行录音并在真正的 iPad 上播放录音?我只想拥有那个代码。

小智 6

输出文件路径扩展名必须与 AVFormatIDKey 同步

对于.wav

 let recordSettings:[String:Any] = [AVFormatIDKey:kAudioFormatLinearPCM,
                          AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey:320000,
                          AVNumberOfChannelsKey:2,
                          AVSampleRateKey:44100.0 ] as [String : Any]
Run Code Online (Sandbox Code Playgroud)

对于.m4a

let recordSettings:[String:Any] = [AVFormatIDKey:kAudioFormatAppleLossless,
                          AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue,
                          AVEncoderBitRateKey:320000,
                          AVNumberOfChannelsKey:2,
                          AVSampleRateKey:44100.0 ] as [String : Any]
Run Code Online (Sandbox Code Playgroud)


eta*_*luz 2

看来我从未将录制会话设置为活动状态。我希望错误描述更好一些。

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
    }
  }
Run Code Online (Sandbox Code Playgroud)