AVSpeechUtterance - Swift - phrase initialization

So, I'm just trying to use AVSpeechSynthesizer with Swift. I cannot figure out how to set the phrase for 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 a phrase to say

The documentation says init (string string: String!), But I can't figure out where to put it

+7
ios8 swift xcode6
source share
2 answers

The code is OK, the speech string is set correctly. However, the problem is that AVSpeechUtterance does not work as expected on the beta version of iOS 8. I suggest writing a bug report here .

The code works great on an iOS 7.1 device and simulator.

+5
source share

Yup, his mistake. For iOS8 GM, it seems the first attempt to get AVSpeechSynthesizer to speak AVSpeechUtterance will lead to silence.

My workaround is to get AVSpeechSynthesizer to speak a one-character statement immediately after it is initialized. It will be quiet, and after that your AVSpeechSynthesizer will work as usual.

In Objective-C:

 AVSpeechUtterance *bugWorkaroundUtterance = [AVSpeechUtterance speechUtteranceWithString:@" "]; bugWorkaroundUtterance.rate = AVSpeechUtteranceMaximumSpeechRate; [self.speechSynthesizer speakUtterance:bugWorkaroundUtterance]; 
+2
source share

All Articles