如何为连接节点获得正确的 AVAudioFormat

How to get right AVAudioFormat for connecting nodes

这段代码让我崩溃了

//  ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController {
    var engine:AVAudioEngine!
    var EQNode:AVAudioUnitEQ!

    override func viewDidLoad() {
        engine.reset()
        let Format = engine.inputNode.outputFormat(forBus: 0)

        print("channelcount:",engine.inputNode.inputFormat(forBus: 0).channelCount)

//----->Start CRASH App stoped here
        engine.connect(engine.inputNode, to: EQNode, format: Format)
        engine.connect(EQNode, to: engine.mainMixerNode, format: Format)

        var error: NSError?
        engine.prepare()
        print("done prepare")
        do {
            try engine.start()
        } catch {
            print(error)
        }
        print("done start")
    }
}

如果我将 Format 更改为 nil,它会使我的应用无法运行但不会崩溃。 所有这些在 Xcode 模拟器上工作得很好,没有错误。 但是在真实的iOS设备中(我用的是iPad2019)测试会崩溃。

关于我的应用程序的详细信息:在均衡器中实时调整麦克风并实时显示均衡的声音。

错误:

SelfhearEQ[3532:760180] [aurioc] AURemoteIO.cpp:1086:Initialize: failed: -10851 
    (enable 1, outf< 2 ch,      0 Hz, Float32, non-inter> inf< 1 ch,  44100 Hz, Float32>)

channelcount: 0
2019-10-22 18:01:29.891748+0700 SelfhearEQ[3532:760180] [aurioc] AURemoteIO.cpp:1086:Initialize: failed: -10851 
(enable 1, outf< 2 ch,      0 Hz, Float32, non-inter> inf< 1 ch,  44100 Hz, Float32>)
2019-10-22 18:01:29.892326+0700 SelfhearEQ[3532:760180] [avae]            
    AVAEInternal.h:76    required condition is false: [AVAudioEngineGraph.mm:2127:_Connect: (IsFormatSampleRateAndChannelCountValid(format))]
2019-10-22 18:01:29.896270+0700 SelfhearEQ[3532:760180] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)'

我找到了这个问题的答案,它与导致此错误的格式无关。 查看我修复的其他问题。