In my app I have a behavior where it can happen that 2 AVSpeech synthesizers are playing at the same time and would therefore overlap. In this case, is there a way to cancel all voice output currently being played as soon as a new voice output is started? Thanks very much! This is my code:
func makeVoiceOutput(_ text: String) {
let spech = AVSpeechUtterance(string: text)
spech.voice = AVSpeechSynthesisVoice(language: Locale.current.languageCode)
let synth = AVSpeechSynthesizer()
synth.speak(spech)
}
The
AVSpeechSynthesizer
documentation https://developer.apple.com/documentation/avfaudio/avspeechsynthesizer/ says:So you should add your speech texts to an instance of
AVSpeechSynthesizer
that you create once, and not create a new one every time in your func. That should lead to queueing the spoken texts one after another.Alternatively using
isSpeaking
andstopSpeaking
you can also stop the current speech output.