这很可能是我对swift一无所知,但我很困惑..
我有以下(简化的)代码-注意,我已经从它中提取了很多东西,因为它(通常)的功能与预期的一样:
engine = AVAudioEngine() //engine being a class variable
AVAudioUnit.instantiate(with: mixerDesc, options: [.loadOutOfProcess], completionHandler: {(audioUnit, auError) in
//configure matrix mixer
self.matrixMixer = au
engine.attach(au)
print("make connections")
engine.connect(engine.inputNode, to: au, format: inputFormat)
engine.connect(au, to: engine.mainMixerNode, format: inputFormat)
print("connections done")
try! engine.start()
} 此代码在99%的情况下都可以正常工作,但是在某些情况下(主要是在默认输入/输出设备发生更改时),在建立连接时会生成错误。我不明白的是,这个错误会导致函数在错误发生时退出(当建立连接时),但不会导致应用程序终止-我断定这是因为错误正在杀死该线程。
有什么方法可以捕捉到这个错误吗?我尝试在函数的开头添加一个defer {},但也没有调用它。这很令人沮丧,因为这意味着应用程序正在失去同步,而我不知道如何捕捉它。
仅供参考,抛出的错误是:
2020-05-31 22:33:22.219189+0100 StageSound[20887:289670] [General] required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)
2020-05-31 22:33:22.221812+0100 StageSound[20887:289670] [General] (
0 CoreFoundation 0x00007fff332f1be7 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6c0c95bf objc_exception_throw + 48
2 CoreFoundation 0x00007fff3331ad98崩溃日志还在继续..
发布于 2020-06-02 01:42:11
看起来像是Objective-C代码抛出了一个NSException。您不能直接从Swift捕获NSExceptions,但有一种方法可以在Objective-C和Swift之间创建一座桥梁。通过这种方式,您可以将NSException转换为Swift能够理解的内容(例如NSError)。
有关此桥接的详细信息,请查看:https://stackoverflow.com/a/36454808
https://stackoverflow.com/questions/62122790
复制相似问题