How to know when AVSpeechSynthesizer has finished speaking all queued utterances. Swift 4.2

Viewed 352

I am trying do use the isSpeaking property to accomplish this. However, it returns false after the first utterance is spoken and therefore fires of the desired code too early.

According to Apple Dev Documentation isSpeaking:

Returns true if the synthesizer is speaking or has utterances enqueued to speak, even if it is currently paused. Returns false if the synthesizer has finished speaking all utterances in its queue or if it has not yet been given an utterance to speak.

So, I don't really understand why it returns false after one utterance when there is more queued up.

I have also tried using the didFinish utterance method, but it also fires of the code after the first utterance.

Is there a way to know when AVSpeechSynthesizer has finished speaking all queued utterances as in my code example below? I am new to programming so any help accomplishing this would be great. Thanks!

var i = 0
    while i < array.count {

        let utterance = "Let's talk about \(array[i])."
        i += 1

        speechSynthesizer.speak(utterance) }
1 Answers

Actually, there's currently (iOS 12, Swift 5.0) no available information about the AVSpeechSynthesizer queue which makes us "blind" about its content.

As you provide utterances to the speaker, the only way to know what's inside the queue is to follow the start and the end of each utterance thanks to the AVSpeechSynthesizerDelegate protocol methods.

Related