gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:09:48 25 4
gpt4 key购买 nike

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

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

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

最佳答案

我就此问题向 Apple 提交了反馈(audioRecorder.record() 在后台返回 false),得到的答案是这是一个新的隐私保护限制,即他们不会修复它。

“提到的行为是一个变化,但 future 的行为是正确的,尝试在后台开始之前未录制的音频录制(然后被调用或 Siri 打断)将不起作用(本质上是尝试开始录制)后台随机录音将不再起作用)。这是12.4中引入的新的隐私保护限制。”

关于当应用程序在后台时,iOS 12.4 AVAudioRecorder.record 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57362848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com