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)
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With