【问题标题】:AVSpeechSynthesizer stops Working after something like 200 callsAVSpeechSynthesizer 在 200 次通话后停止工作
【发布时间】:2020-09-22 04:53:12
【问题描述】:

我使用 AVSpeechSynthesizer 来朗读文本,在使用它进行 200 次之后,一些奇怪的行为正在发生(在 macOS 模拟器中,也在 iPad 模拟器中) 在 200 次调用 (synthesizer.speak(utterance)) 之后,一切都按预期工作,它突然进入他的 AVSpeechSynthesizerDelegte 的 didCancel 方法(),并且永远停止工作!,甚至没有再次调用 didCancel,或者任何其他委托方法都称为synthesizer.speak(utterance),只是注意一下。

我尝试将合成器引用分配给 didCancel 方法内的新 AVSpeechSynthesizer 实例,但它对它没有影响,在 didCancel 方法累积之后,“让它回来”的唯一事情是再次运行应用程序,然后在使用合成器 speak 方法进行 200 次调用后再次运行,它再次损坏。 这是我的代码:

   var synthesizer:AVSpeechSynthesizer! = AVSpeechSynthesizer()
       override func viewDidLoad() { 
synthesizer.delegate = self
}

    func speakText(utterance: AVSpeechUtterance ) {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: .mixWithOthers)
            try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
        } catch {
            print("audioSession properties weren't set because of an error.")
        }
        synthesizer.speak(utterance)
        
        print("speak now \(utterance.speechString)")
    }


  func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
            
        print(" AVSpeechSynthesizerDelegate cancel")
      

        }

【问题讨论】:

  • 我也遇到过同样的问题。

标签: swift xcode avspeechsynthesizer


【解决方案1】:

我设法解决了这个问题,我从 if else 语句中调用 speakText(utterance: AVSpeechUtterance) 方法,该语句根据需要更改话语文本,然后在每个块内调用函数。(在 ifelse 块)

我所做的是将话语置于块之外,并将其分配给 nil:var utterance:AVSpeechUtterance。 然后在if else 块中,我为它分配了适当的值。

然后在块之外,我检查了该值是否已设置,以及是否已设置(不是零)。所有这些功能都在从同步的全局线程块触发的函数中变形。像这样

DispatchQueue.global(qos: .userInteractive).sync{ [unowned self] in
            
            self.userDidEnterText(text: text)
            
        }

希望这种句柄可以为将来面临该问题的人提供一个想法或方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多