简体   繁体   中英

iOS 12.4 AVAudioRecorder.record is returning false when app is in the background

First of all, this error only occurs in the latest 12.4 release on iOS. The issue does NOT occur in the simulator and must be run on a device. The issue is that the call to record on the AVAudioRecorder is returning false once the app goes into the background. In all previous versions of iOS, this would not happen. The info.plist is updated with the NSMicrophoneUsageDescription tag, and the capabilities for the app includes Audio background mode.

I have written a small ViewController that shows the issue. Steps to recreate:

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
   }
}

Since the capability for the app to record in the background has been set, I expect the call to record on the AVAudioRecorder to return true

I filed a feedback to Apple about this (audioRecorder.record() returns false while in background) and got the answer that it's a new privacy protection restriction, ie they will not fix it.

"The behaviour mentioned is a change but is the correct behaviour going forward, attempting to start audio recording in the background which had not been recording previously (and then interrupted by a call or Siri) will not work (essentially trying to start recording from the background randomly will no longer work). This is a new a privacy protection restriction introduced in 12.4."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM