ios - AVSpeechSynthesizer 只工作一次

标签 ios swift uicollectionview text-to-speech avspeechsynthesizer

我有一个使用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/

相关文章:

Swift 和 Firebase 实时数据库 : How do I get the number of objects?

iOS - UIButton 操作的选择器中的硬编码值

ios - 屏幕旋转后 UIImageView 和 UIButton 图像困惑

ios - swift 从模型(networkClient)通知ViewController

ios - 2 带有动态内容的 ScrollView 内的 UIcollectionview

ios - 使用 [UICollectionView performBatchUpdates :] with a UICollectionViewFlowLayout that has UIDynamics

ios - 如何访问 UICollectionView 中页眉或页脚的特定实例?

iOS:禁用 subview 的自动旋转

ios - 编写一个适应每个属性的 ivar 是最佳实践吗?

ios - ScrollView 内的 ForEach 不占用整个宽度