AVSpeechSynthesizer只能工作一次
问题描述:
我有一个使用UICollectionView
的快速应用程序。 UICollectionView
中的每个单元都有一个显示一些文本的视图控制器。我添加了两个按钮来启动并暂停AVSpeechSynthesizer
阅读文本。这两个功能是:AVSpeechSynthesizer只能工作一次
func playclicked(){
if isInitial {
let textString = (postTitle.text ?? "") + ".\n\n" + postContentWeb.stringByEvaluatingJavaScript(from: "document.body.textContent")!
synth = AVSpeechSynthesizer()
myVoice = AVSpeechSynthesisVoice(language: "de-DE")
myUtterance = AVSpeechUtterance(string: textString)
myUtterance.voice = myVoice
synth.speak(myUtterance)
synth.delegate = self
navigationItem.rightBarButtonItems = isPlayingButtons
isInitial = false
} else {
synth.continueSpeaking()
navigationItem.rightBarButtonItems = isPlayingButtons
}
}
func pauseclicked(){
synth.pauseSpeaking(at: .word)
navigationItem.rightBarButtonItems = isStoppedButtons
}
一个视图起点和阅读作品的停止,但如果我选择另一个单元格的语音输出停止在所有的工作,即使我重新选择它之前就已经工作的细胞。只有重新启动应用程序才能解决问题。
答
我在玩AVSpeechSynthesizer API时遇到过这个问题。
当播放和暂停syntesizer,您使用的方法正确:
continueSpeaking()和pauseSpeaking(在:.word)
当你想之前的合成器说完驳回的ViewController ,请改用此方法:
synthesizer.stopSpeaking(at :.immediate)