将输入节点连接到 AVAudioSinkNode 时出错

error while connecting input node to AVAudioSinkNode

连接节点时出错

let sourceNode = AVAudioSinkNode { (test1, frameCount, audioBufferList) -> OSStatus in
    print("callback", self.testInteger)
    return noErr
}
audioEngine.attach(sourceNode)
audioEngine.connect(audioEngine.inputNode, to: sourceNode, format: nil)

[aurioc] AURemoteIO.cpp:1086:初始化:失败:-10851(启用 1,outf <2 ch,0 Hz,Float32,非内部> inf <2 ch,0 Hz,Float32,非间>)

然后 try audioEngine.start ()

时出错

[avae] AVAEInternal.h: 88 所需条件为假: [AVAudioEngineGraph.mm:1415:Initialize: (IsFormatSampleRateAndChannelCountValid (inputHWFormat))] [avae] AVAudioEngine.mm:160 Engine @ 0x2836a8940: 无法初始化,错误 = -10875 [avae] AVAEInternal.h: 88 所需条件为 false: [AVAudioEngineGraph.mm:1415:Initialize: (IsFormatSampleRateAndChannelCountValid (inputHWFormat))]

我也试过了

audioEngine.connect (audioEngine.inputNode, to: sourceNode, format: audioEngine.inputNode.inputFormat (forBus: 0))

audioEngine.connect (audioEngine.inputNode, to: sourceNode, format: audioEngine.inputNode.outputFormat (forBus: 0))

在这两种情况下,应用程序会因错误而崩溃 “由于未捕获的异常 'com.apple.coreaudio.avfaudio',正在终止应用程序,原因:'所需条件为假:IsFormatSampleRateAndChannelCountValid(格式)”

我发现有必要将音频会话的类别放在 playAndRecord 中的问题

try audioSession.setCategory (.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)

在这种情况下,此选项有效

audioEngine.connect (audioEngine.inputNode, to: sourceNode, format: audioEngine.inputNode.inputFormat (forBus: 0))