当应用程序在后台运行时,iOS 12.4 AVAudioRecorder.record返回false

时间:2019-08-05 16:47:27

标签: ios swift avaudioplayer

首先,此错误仅发生在iOS上的最新12.4版本中。该问题不会在模拟器中发生,必须在设备上运行。问题是,一旦应用程序进入后台,在AVAudioRecorder上录制的调用将返回false。在所有以前的iOS版本中,都不会发生这种情况。 info.plist使用NSMicrophoneUsageDescription标签进行了更新,并且该应用程序的功能包括音频背景模式。

我写了一个小的ViewController来显示问题。重建步骤:

1) Update the info.plist file with the NSMicrophoneUsageDescription tag so that the app gets permission to use the microphone 2) Update the app capabilities to set Audio background mode 3) Run the application 4) Send the application to the background 5) The current recording will finish, but the call to start a new recording will fail.


class ViewController: UIViewController, AVAudioRecorderDelegate {
   var recordLabel: UILabel!
   var recordingSession: AVAudioSession!
   var audioRecorder: AVAudioRecorder!
   var count: Int = 0

   override func viewDidLoad() {
       super.viewDidLoad()

       recordingSession = AVAudioSession.sharedInstance()

       do {
           try recordingSession.setCategory(.playAndRecord, mode: .default)
           try recordingSession.setActive(true)
           recordingSession.requestRecordPermission() { [unowned self] allowed in
               DispatchQueue.main.async {
                   if allowed {
                       self.loadRecordingUI()
                   } else {
                       print("No permissions!!!")
                   }
               }
           }
       } catch {
           print("Exception in viewDidLoad!!!")
       }
   }

   func loadRecordingUI() {
       super.viewDidLoad()

       recordLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 21))
       recordLabel.center = CGPoint(x: 160, y: 285)
       recordLabel.textAlignment = .center
       recordLabel.text = "Waiting...."
       self.view.addSubview(recordLabel)

       setupRecorder()
       startRecording();
   }

   func setupRecorder() {
       let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")

       let settings = [
           AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
           AVSampleRateKey: 12000,
           AVNumberOfChannelsKey: 1,
           AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
       ]

       do {
           audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
           audioRecorder.delegate = self
       } catch {
           print("Exception thrown in setupRecorder")
       }
   }

   func startRecording() {
       count += 1

       let ret = audioRecorder.record(forDuration: 10)  //record for 10 seonds

       let txt = "Record returned " + ret.description + " for #\(count)"
       recordLabel.text = txt
       print(txt)
   }

   func getDocumentsDirectory() -> URL {
       let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
       return paths[0]
   }

   func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
       startRecording()  //immediately start recording again
   }
}

由于已设置了应用程序在后台录制的功能,所以我希望在AVAudioRecorder上录制的调用返回true

1 个答案:

答案 0 :(得分:0)

我向Apple提交了有关此问题的反馈(audioRecorder.record()在后台时返回false),并得到了答案,它是新的隐私保护限制,即他们将无法修复它。

“提到的行为是一种更改,但是这是正确的行为,尝试在以前从未录制过的后台开始录音(然后被呼叫或Siri中断)将无法正常工作(本质上是尝试开始从后台随机录制将不再起作用。)这是12.4中引入的新的隐私保护限制。”