繁体   English   中英

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

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

首先,此错误仅发生在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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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