问题
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