0

背景

我正在创建一个SpeechSynth在大多数设备上运行良好的简单组件,但不知何故在 FireFox android 上,SpeechSynthUtterance它将调度事件,并且在onerror事件上没有定义错误属性

声音已加载;这不是不确定声音的问题。我无法深入了解这一点,因为错误很简单undefined

再生产

const MAX_LOAD_VOICES = 5
let loadVoiceCount = 0

const loadVoices = (callback) => {
  const speech = window.speechSynthesis
  const voices = speech.getVoices()

  if (voices.length > 0) {
    return callback({ speech, voices })
  }

  if (++loadVoiceCount > MAX_LOAD_VOICES) {
    throw new Error(`Failed to load speech synthesis voices, after ${loadVoiceCount} retries.`)
  }

  return setTimeout(() => loadVoices(callback), 100)
}

loadVoices(({ speech, voices }) => {
  const utterance = new window.SpeechSynthesisUtterance('Hello world')
  utterance.voice = voices[0] // assume this is the voice we want
  utterance.onend = function () {
    console.log('SpeechSynth successfully ended')
  }

  utterance.onerror = function (event) {
    console.log('SpeechSynth failed')
    console.debug(event)
  }
  
  speech.speak(utterance)
})

预期的

这个脚本应该运行得很好(只要用你的浏览器试试)

Firefox Android 上的结果

utterance.onerror 事件将被触发,没有定义错误,也没有说出文本。

记录的错误事件的输出 ( console.debug(event)):

{
bubbles: false
cancelBubble: false
cancelable: false
charIndex: 0
charLength: null
composed: false
currentTarget: null
defaultPrevented: false
elapsedTime: 0
eventPhase: 0
explicitOriginalTarget: SpeechSynthesisUtterance { text: "Hello world", volume: 1, rate: 1, … }
isTrusted: true
name: ""
originalTarget: SpeechSynthesisUtterance { text: "Hello world", volume: 1, rate: 1, … }
returnValue: true
srcElement: SpeechSynthesisUtterance { text: "Hello world", volume: 1, rate: 1, … }
target: SpeechSynthesisUtterance { text: "Hello world", volume: 1, rate: 1, … }
timeStamp: 2591
type: "error"
utterance: SpeechSynthesisUtterance { text: "Hello world", volume: 1, rate: 1, … }
<get isTrusted()>: function isTrusted()
<prototye>: SpeechSynthesisEventPrototype { utterance: Getter, charIndex: Getter, charLength: Getter, … }
}

浏览器兼容性也应该没有问题,因为我在 Android 11 上运行 FF Mobile 88.0.1

我是否错过了一些“特别”的东西来创造话语?

4

0 回答 0