我正面临以下问题,希望其他人能够解决并提供解决方案:
AVAudioEngine
访问麦克风。在iOS 12.4之前,每次音频路由更改时,我都可以重新启动AVAudioEngine
图以对其进行重新配置,并确保输入/输出音频格式适合新的输入/输出路由。由于iOS 12.4中引入的更改,因此在应用程序后台运行时(除非在中断之后)不再可以启动(或重新启动)AVAudioEngine
图形。 Apple现在尝试执行此操作时抛出的错误是:
2019-10-03 18:34:25.702143+0200 [1703:129720] [aurioc] 1590: AUIOClient_StartIO failed (561145187)
2019-10-03 18:34:25.702528+0200 [1703:129720] [avae] AVAEInternal.h:109 [AVAudioEngineGraph.mm:1544:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): error 561145187
2019-10-03 18:34:25.711668+0200 [1703:129720] [Error] Unable to start audio engine The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error 561145187.)
我猜苹果在这里关闭了一个安全漏洞。因此,现在我删除了更改音频路径(例如,连接了蓝牙耳机)时重新启动图形的代码。
.AVAudioEngingeConfigurationChange
通知,以允许集成应用程序对格式更改做出反应。从一开始,这就是我应该用来处理I / O格式更改的方法,而不是强行强制重新启动图形。根据Apple文档-“当音频引擎的I / O单元观察到音频输入或输出硬件的通道数或采样率发生变化时,音频引擎将停止,自行初始化并发出此通知。”(see the docs here) 。当应用程序在后台运行时发生这种情况时,由于第1点,我无法使用正确的音频I / O格式启动音频引擎。因此,最重要的是,通过关闭一个安全漏洞,Apple在后台运行应用程序时引入了对音频I / O格式更改做出反应的错误。还是我错过了什么?
我附上代码片段以更好地描述问题。
class RCAudioEngine {
private let audioEngine = AVAudioEngine()
init() {
setup()
start()
NotificationCenter.default.addObserver(self, selector: #selector(handleConfigurationChange), name: .AVAudioEngineConfigurationChange, object: nil)
}
@objc func handleConfigurationChange() {
//attempt to call start()
//or to audioEngine.reset(), setup() and start()
//or any other combination that involves starting the audioEngine
//results in an error 561145187.
//Not calling start() doesn't return this error, but also doesn't restart
//the recording.
}
public func setup() {
//Setup nodes
let inputNode = audioEngine.inputNode
let inputFormat = inputNode.inputFormat(forBus: 0)
let mainMixerNode = audioEngine.mainMixerNode
//Mute output to avoid feedback
mainMixerNode.outputVolume = 0.0
inputNode.installTap(onBus: 0, bufferSize: 4096, format: inputFormat) { (buffer, _) -> Void in
//Do audio conversion and use buffers
}
public func start() {
RCLog.debug("Starting audio engine")
guard !audioEngine.isRunning else {
RCLog.debug("Audio Engine is already running")
return
}
do {
audioEngine.prepare()
try audioEngine.start()
} catch {
RCLog.error("Unable to start audio engine \(error.localizedDescription)")
}
}
}
我只看到iOS 12.4中已安装的修复程序。我不确定是否会导致此问题。
带有发行说明https://developer.apple.com/documentation/ios_ipados_release_notes/ios_12_4_release_notes
“解决了以下问题:在给定运行的第一个泄漏检查之后,在Leaks工具下在iOS 12.2或更高版本中运行应用程序会导致随机数量的假阳性泄漏。您可能仍会在Simulator中遇到此问题,或在MacOS应用中使用Xcode 10.2和更高版本的乐器时(48549361)“
如果您是签名开发人员,则可以向Apple提出问题。如果有缺陷,他们可能会为您提供帮助。
您还可以在即将发布的iOS版本中进行测试,以检查您的代码在将来的版本中是否可用(使用Apple Beta程序)