我有一个使用UICollectionView
的快速应用程序。 UICollectionView
中的每个单元格都有一个显示一些文本的 View Controller 。我添加了两个按钮来启动和暂停 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
}
对于一个 View ,阅读的开始和停止有效,但如果我选择另一个单元格,语音输出将完全停止工作,即使我重新选择之前一直工作的单元格。只有重新启动应用程序才能解决问题。
最佳答案
我之前在使用 AVSpeechSynthesizer API 时遇到过这个问题。
播放和暂停合成器时,您正确使用的方法是:
继续说话()和暂停说话(在:.word)
当您想在合成器结束说话之前关闭 ViewController 时,请使用此方法:
synthesizer.stopSpeaking(at: .immediate)
关于ios - AVSpeechSynthesizer 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44650111/