Join 2 different labels for text to speech conversion (swift3)

倖福魔咒の 提交于 2020-01-24 19:35:09

问题


Using the speech to text feature I can easily get one label to be spoken. But I want utterance2 to be joined to utterance. I want utterance to be spoken first then when it is finished for utterance2 to be spoken right after.

     let utterance = AVSpeechUtterance(string: dptext.text!)
     let utterance2 = AVSpeechUtterance(string: dptext2.text!)

     let synthesizer = AVSpeechSynthesizer()
     synthesizer.speak(utterance)

回答1:


I think the simplest way to handle this situation is to combine the two string with space.

let combineString = dptext.text! + " " + dptext2.text!
let utterance = AVSpeechUtterance(string: combineString)
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)



回答2:


I want utterance to be spoken first then when it is finished for utterance2 to be spoken right after.

There's no need in making a join for the two utterances to be spoken : the most important thing to remember is to retain your AVSpeechSynthesizer instance until full speech is done.

let synthesizer = AVSpeechSynthesizer()

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let utterance = AVSpeechUtterance(string: dptext.text!)
    let utterance2 = AVSpeechUtterance(string: dptext2.text!)

    synthesizer.speak(utterance)
    synthesizer.speak(utterance2)
}

Following this rationale, you can:

  • Add pre/post delays to the utterances when speaking.
  • Make as many utterances speak as you want.

Each one of the utterances will be put in the speech synthesizer queue and will be spoken in the order it's been received.



来源:https://stackoverflow.com/questions/44703430/join-2-different-labels-for-text-to-speech-conversion-swift3

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