【问题标题】:AVAudioEngine connect - code exiting function without throwing errorAVAudioEngine 连接 - 代码退出函数而不抛出错误
【发布时间】:2020-05-31 21:36:00
【问题描述】:

这很可能是我对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

崩溃日志继续..

【问题讨论】:

    标签: swift macos cocoa core-audio


    【解决方案1】:

    看起来像是从 Objective-C 代码中抛出了 NSException。 你不能直接从 Swift 中捕获 NSExceptions,但是有一种方法可以在 Objective-C 和 Swift 之间建立一座桥梁。这样你就可以将 NSException 转换成 Swift 可以理解的东西(例如 NSError)。

    有关此桥接的详细信息,请查看:https://stackoverflow.com/a/36454808

    【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 2020-12-12
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多