I'm recording audio with AVAudioEngine.inputNode. Everything worked fine until I needed to play an other audio while recording. I'm creating AVPlayer and settings player item with custom AVCompositions with many tracks, it plays fine. But when I tried to record audio after initializing AVPlayer, I've got a crash on installing the tap:
[avae] AVAEInternal.h:76 required condition is false: [AVAudioIONodeImpl.mm:1093:SetOutputFormat: (format.sampleRate == hwFormat.sampleRate)]
Then I added the following prints:
let inputNode = audioEngine.inputNode
print(inputNode.inputFormat(forBus: 0))
let recordingFormat = inputNode.inputFormat(forBus: 0)
print(recordingFormat, inputNode.inputFormat(forBus: 0))
This the output is this:
<AVAudioFormat 0x2820da6c0: 1 ch, 44100 Hz, Float32>
<AVAudioFormat 0x2820da800: 1 ch, 48000 Hz, Float32> <AVAudioFormat 0x2820da800: 1 ch, 48000 Hz, Float32>
Or this:
<AVAudioFormat 0x2820da6c0: 1 ch, 44100 Hz, Float32>
<AVAudioFormat 0x2820da800: 1 ch, 44100 Hz, Float32> <AVAudioFormat 0x2820da800: 1 ch, 48000 Hz, Float32>
Ofc the code works with this prints, because it looks like after one(or two) calls of inputFormat the node is reinitializing. Why does it happens and what's the good way to fix it?
I don't know the exact reasons why this happened and how to solve it in this particular circumstances, but I've switched from AVPlayer
to AVAudioEngine
as an audio player too, and it helped.