Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVSpeechUtterance - Swift - initializing with a phrase

Tags:

swift

xcode6

ios8

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

like image 969
ShadowDES Avatar asked Jun 29 '14 02:06

ShadowDES


2 Answers

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.

like image 199
vladof81 Avatar answered Nov 02 '22 11:11

vladof81


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];
like image 40
user848555 Avatar answered Nov 02 '22 12:11

user848555