Can you tell me the cause of the crash that occurs when AVSpeechSynthesizer is stopped?

We are providing the ability to speak text in our app using AVSpeechSynthesizer .

Stop the AVSpeechSynthesizer at random I get a crash like the screenshot below.

What causes this error to occur?

It happens occasionally during testing, but it doesn't go away even if I fix it in various ways.

Is this a bug in iOS or is there something I need to fix or add?

If a crash occurs, the message below is also displayed in the log window during debugging.

[AXTTSCommon] _BeginSpeaking: speech cancelled error: Error Domain=TTSErrorDomain Code=-4005 "(null)"

[AXTTSCommon] _BeginSpeaking: couldn't begin playback

TTS Create Code

let utterance = AVSpeechUtterance(string: playText)

if Singletone.sharedInstance().ttsIdentifier != nil {

       utterance.voice = AVSpeechSynthesisVoice(identifier: Singletone.sharedInstance().ttsIdentifier!)

}
else {

        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
}

        

utterance.rate = 0.4

utterance.pitchMultiplier = 1.0


utterance.preUtteranceDelay = -1.0

utterance.postUtteranceDelay =  -1.0

utterance.volume = 1.0

        

if self.synthesizer == nil {
           
       self.synthesizer = AVSpeechSynthesizer()

       self.synthesizer!.delegate = self
}

        

self.synthesizer!.speak(utterance)

TTS Stop Code

self.synthesizer!.stopSpeaking(at: .immediate)
self.synthesizer!.delegate = nil
self.synthesizer = nil

I use Speech intensively in an app and user can interrupt speech at any time, during play. No crash, neither in simulator or device (iOS 16 / Xcode 14).

Could you show the code where the crash occurs as well as context (how is speechSynthetiser created, kept as a reference, stopped…)

When you stop, I assume you test for nil ?

if self.synthesizer != nil {
  self.synthesizer!.stopSpeaking(at: .immediate)
  self.synthesizer!.delegate = nil
  self.synthesizer = nil
}

Which delegate functions do you use ?

Can you tell me the cause of the crash that occurs when AVSpeechSynthesizer is stopped?
 
 
Q