AVSpeechSynthesizer write method get audio buffer and play audio deosn't work

I am using AVSpeechSynthesizer to get audio buffer and play, I am using AVAudioEngine and AVAudioPlayerNode to play the buffer. But I am getting error.

[avae] AVAEInternal.h:76 required condition is false: [AVAudioPlayerNode.mm:734:ScheduleBuffer: (_outputFormat.channelCount == buffer.format.channelCount)] 2023-05-02 03:14:35.709020-0700 AudioPlayer[12525:308940] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _outputFormat.channelCount == buffer.format.channelCount'

Can anyone please help me to play the AVAudioBuffer from AVSpeechSynthesizer write method?

Having the same issue. Did you find a solution?

The write method of AVSpeechSynthesizer would normally be used to save the synthesized speech to an audio file (using AVAudioFile, for example). If you would like AVSpeechSynthesizer to produce sound, it is easier to create an utterance and call the speak method:

let synthesizer = AVSpeechSynthesizer()

func speakText(text: String) {
    guard let voice = AVSpeechSynthesisVoice(language: nil) else { return }
    let utterance = AVSpeechUtterance(string: text)
    utterance.voice = voice
    synthesizer.speak(utterance)
}

The error you are describing has the following reason:

'required condition is false: _outputFormat.channelCount == buffer.format.channelCount'

This is likely happening because the format of the buffers produced by AVSpeechSynthesizer does not match the format you used to connect your AVAudioPlayerNode to your engine's main mixer or output node:

var format: AVAudioFormat? // <-- this format needs to match the format of the audio buffers you will later schedule
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: format)
AVSpeechSynthesizer write method get audio buffer and play audio deosn't work
 
 
Q