So I'm just trying to use the AVSpeechSynthesizer with Swift. I cannot figure out how to set the phrase for the AVSpeechUtterance.
@IBAction func buttonSpeakClicked(sender:UIButton)
{
var mySpeechSynthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer()
var myString:String = "This is the phrase to say"
var mySpeechUtterance:AVSpeechUtterance = AVSpeechUtterance(string:myString)
println("\(mySpeechUtterance.speechString)")
println("My string - \(myString)")
mySpeechSynthesizer .speakUtterance(mySpeechUtterance)
}
First println - Nil
Second println - This is the phrase to say
Documentation says init(string string: String!), but I can't figure out where to put it
The code is fine, speech string is set correctly. However issue is that AVSpeechUtterance is not working as expected on iOS 8 Beta. I suggest file a bug report here.
The code works fine on iOS 7.1 device and simulator.
Yup, its a bug. As of iOS8 GM, it seems the first attempt to get AVSpeechSynthesizer to speak an AVSpeechUtterance will result in silence.
My workaround is to make AVSpeechSynthesizer speak a single-character utterance immediately after it is initialized. This will be silent, and afterwards your AVSpeechSynthesizer will work as normal.
In Objective-C:
AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "];
bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate;
[self.speechSynthesizer speakUtterance:bugWorkaroundUtterance];
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