Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pause and resume AVSpeechSynthesizer / AVSpeechUtterance in swift?

I have implemented text-to-speech in my app and it works fine with the code I currently use. Basically an algo creates a text, and then if the user clicks on the UIButton the text is being spoken.

Challenge: I want to enable the same UIButton to pause the synthesizer, if the button has already been tapped (i.e. text is currently being spoken) and then resume speaking where it left off, if the button is being tapped again.

I know there are a few functions in the AVFoundation Reference but I am unable to implement them correctly.

Does anyone know how to do this in Swift?

import UIKit
import AVFoundation


    @IBOutlet var generatedText: UILabel!

@IBAction func buttonSpeakClicked(sender: UIButton){
    var mySpeechSynthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer()
    var mySpeechUtterance:AVSpeechUtterance = AVSpeechUtterance(string:generatedText.text)
    mySpeechUtterance.rate = 0.075

mySpeechSynthesizer .speakUtterance(mySpeechUtterance)
}
like image 511
KML Avatar asked Sep 25 '14 12:09

KML


1 Answers

AVSpeechSynthesizer Class Reference

Have you tried these methods? - pauseSpeakingAtBoundary: and - continueSpeaking

And these are some properties, (paused and speaking ) that can help you determine the state of the synthesizer.

Code like this should work: mySpeechSynthesizer.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate)

like image 191
lancy Avatar answered Sep 18 '22 20:09

lancy