AVSpeechUtterance - Swift - initializing with a phrase

让人想犯罪 __ 提交于 2019-12-18 16:48:21

问题


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


回答1:


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.




回答2:


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];


来源:https://stackoverflow.com/questions/24472766/avspeechutterance-swift-initializing-with-a-phrase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!