gpt4 book ai didi

ios - AVSpeechSynthesizer 错误 : An AVSpeechUtterance shall not be enqueued twice

转载 作者:可可西里 更新时间:2023-11-01 01:55:56 30 4
gpt4 key购买 nike

我有点卡住了。

这是我的代码:

let speaker = AVSpeechSynthesizer()
var playQueue = [AVSpeechUtterance]() // current queue
var backedQueue = [AVSpeechUtterance]() // queue backup

...

func moveBackward(_ currentUtterance:AVSpeechUtterance) {
speaker.stopSpeaking(at: .immediate)
let currentIndex = getCurrentIndexOfText(currentUtterance)
// out of range check was deleted
let previousElement = backedQueue[currentIndex-1]
playQueue.insert(previousElement, at: 0)
for utterance in playQueue {
speaker.speak(utterance) // error here
}
}

根据文档AVSpeechSynthesizer.stopSpeaking(at:):

Stopping the synthesizer cancels any further speech; in constrast with when the synthesizer is paused, speech cannot be resumed where it left off. Any utterances yet to be spoken are removed from the synthesizer’s queue.

当我在 AVSpeechSynthesizer 队列中插入 AVSpeechUtterance 时,我总是会收到错误消息(AVSpeechUtterance 不得入队两次)。但它应该根据文档停止。

最佳答案

当停止播放器时,话语肯定会从队列中删除。

但是,在您的 moveBackward 函数中,您在 playQueue[0] 处插入了另一个 AVSpeechUterrance,其完整数组表示播放器队列。

假设停止发生在 currentIndex = 2 时,以下快照证明同一对象在队列中被注入(inject)两次:

  • 复制 backedQueue[1],它是 playQueue[1] 的副本 (相同的内存地址)enter image description here

  • playQueue[0] 处插入 backedQueue[1] (原来的 playQueue[1] 变成新的 playQueue[2])enter image description here

不幸的是,正如系统指示的那样,AVSpeechUtterance 不应入队两次,而这正是您在这里所做的:playQueue 索引 0 和 2 处的对象具有相同的内存地址强>.

在索引 0 处插入新对象后的最后一个循环要求语音合成器将所有话语放入其全新队列中......其中两个是相同的。

不是将 playedQueue 复制到 backedQueue (它们的对象都包含相同的内存地址) 或者在两者中附加相同的语句数组,我建议创建不同的话语实例,如下所示:

    for i in 1...5 {

let stringNb = "number " + String(i) + " of the speech synthesizer."
let utterance = AVSpeechUtterance(string: stringNb)
playQueue.append(utterance)

let utteranceBis = AVSpeechUtterance(string: stringNb)
backedQueue.append(utteranceBis)
}

按照这条建议,您应该不会遇到错误AVSpeechUtterance shall not be enqueued twice

关于ios - AVSpeechSynthesizer 错误 : An AVSpeechUtterance shall not be enqueued twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53606746/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com