【问题标题】:SpeechSynthesisUtterance detect onend when only speaking completes once not on cancelSpeechSynthesisUtterance 在仅说话完成一次而不是取消时检测onend
【发布时间】:2022-09-17 21:15:40
【问题描述】:

我正在使用window.SpeechSynthesisUtterance API,我发现当您调用window.speechSynthesis.cancel() 时,它也会立即调用onend 事件。 我只想在讲话结束时做一些具体的事情,而不是在讲话时取消讲话时。

如何检测通话是否由于取消方法调用而结束?

onend 事件给出和事件对象:
onend = event => { }

但在这两种情况下,当演讲结束或被取消时,event.type\'end\'

  • 有关更多上下文,我想在讲话结束后将按钮的文本从 \'mute\' 更改为 \'listen again\',但在取消时不会(例如在模式关闭时)。

标签: javascript text-to-speech webapi speech-synthesis


【解决方案1】:

您可以在end 侦听器中检查SpeechSynthesisEventcharIndex,并将其与SpeechSynthesisEvent.utterance 的长度进行比较。

例如,

utterance.addEventListener('end', (evt) => {
  const { charIndex, utterance } = evt

  if (charIndex + 1 === utterance.text.length) {
    // End fired when utterance finished
  } else {
    // End fired before utterance finished
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多