【问题标题】:AVSpeechSynthesizer only working onceAVSpeechSynthesizer 只工作一次
【发布时间】:2017-08-17 08:23:33
【问题描述】:

我有一个使用 UICollectionView 的 swift 应用程序。 UICollectionView 中的每个单元格都有一个显示一些文本的视图控制器。我添加了两个按钮来开始和暂停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
}

对于开始和停止阅读工作的一个视图,但如果我选择另一个单元格,语音输出将完全停止工作,即使我重新选择了之前工作的单元格。只有重新启动应用程序才能解决问题。

【问题讨论】:

  • 也许this 会有所帮助。

标签: ios swift uicollectionview text-to-speech avspeechsynthesizer


【解决方案1】:

我之前在使用 AVSpeechSynthesizer API 时遇到过这个问题。

在播放和暂停合成器时,您正确使用了以下方法:

continueSpeaking() 和 pauseSpeaking(at: .word)

如果您想在合成器说完之前关闭 ViewController,请改用此方法:

synthesizer.stopSpeaking(at: .immediate)

【讨论】: