当应用程序位于后台时,iOS 12.4 AVAudioRecorder.record返回false
首先,这个错误只发生在iOS上最新的12.4版本中。该问题不会发生在模拟器中,必须在设备上运行。问题在于,一旦应用程序进入后台,在AVAudioRecorder上录制的调用将返回false。在所有以前版本的iOS中,这种情况都不会发生。info.plist使用NSMicrophoneUsageDescription标记更新,应用程序的功能包括音频背景模式 我已经编写了一个小的ViewController来显示这个问题。重新创建的步骤:当应用程序位于后台时,iOS 12.4 AVAudioRecorder.record返回false,ios,swift,avaudioplayer,Ios,Swift,Avaudioplayer,首先,这个错误只发生在iOS上最新的12.4版本中。该问题不会发生在模拟器中,必须在设备上运行。问题在于,一旦应用程序进入后台,在AVAudioRecorder上录制的调用将返回false。在所有以前版本的iOS中,这种情况都不会发生。info.plist使用NSMicrophoneUsageDescription标记更新,应用程序的功能包括音频背景模式 我已经编写了一个小的ViewController来显示这个问题。重新创建的步骤: 1)使用NSMicrophoneUsageDescripti
1)使用NSMicrophoneUsageDescription标记更新info.plist文件,以便应用程序获得使用麦克风的权限
2) 更新应用程序功能以设置音频背景模式
3) 运行应用程序
4) 将应用程序发送到后台
5) 当前录制将完成,但启动新录制的调用将失败。
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中引入的新的隐私保护限制。“True,您不能在iOS 12.4和iOS 13 beta上开始后台录制。所有框架都受到此问题的影响-更多信息请参见此处。请向苹果公司提交一份错误报告,希望他们能尽快解决……上述问题已在iOS 13.1中得到解决,但在尝试连接蓝牙设备同时在后台访问麦克风时仍然存在。我在这里开了一张罚单来描述这个问题:这在13.1中是不固定的。当应用程序处于后台时,呼叫录制仍返回false。有没有其他方法可以在中断后继续录制。你能提供一份提到它是新的隐私政策保护限制的文档吗?我找不到任何关于它的文档,这就是我向苹果提交反馈的原因。收到回复后请在这里发布苹果公司?