Search code examples
swiftavfoundationcore-audioavaudioengine

error while connecting input node to AVAudioSinkNode


I get an error when connecting nodes

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: Initialize: failed: -10851 (enable 1, outf <2 ch, 0 Hz, Float32, non-inter> inf <2 ch, 0 Hz, Float32, non-inter>)

And after that errors when try audioEngine.start ()

[avae] AVAEInternal.h: 88 required condition is false: [AVAudioEngineGraph.mm:1415:Initialize: (IsFormatSampleRateAndChannelCountValid (inputHWFormat))] [avae] AVAudioEngine.mm:160 Engine @ 0x2836a8940: could not initialize, error = -10875 [avae] AVAEInternal.h: 88 required condition is false: [AVAudioEngineGraph.mm:1415:Initialize: (IsFormatSampleRateAndChannelCountValid (inputHWFormat))]

I also tried

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

and

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

in these two cases the application crashes with an error "Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid (format)"


Solution

  • I found the problem it was necessary to put the category of the audio session playAndRecord

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

    under this condition, this option works

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