SpeechSynthesisErrorEvent

Baseline 已广泛支持

此特性已成熟稳定,适用于多种设备和浏览器版本。自 2018 年 10 月起,它已在各浏览器中可用。

SpeechSynthesisErrorEvent 接口是 Web Speech API 的一部分,它包含有关在语音服务中处理 SpeechSynthesisUtterance 对象时发生的任何错误的详细信息。

Event SpeechSynthesisEvent SpeechSynthesisErrorEvent

构造函数

SpeechSynthesisErrorEvent()

创建一个新的 SpeechSynthesisErrorEvent

实例属性

SpeechSynthesisErrorEvent 继承自 SpeechSynthesisEvent 接口,后者又继承自其父接口 Event

SpeechSynthesisErrorEvent.error 只读

返回一个错误代码,指示语音合成尝试中出现的错误。

实例方法

SpeechSynthesisErrorEvent 继承自 SpeechSynthesisEvent 接口,后者继承自其父接口 Event

示例

js
const synth = window.speechSynthesis;

const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");

const voices = synth.getVoices();

// …

inputForm.onsubmit = (event) => {
  event.preventDefault();

  const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
  const selectedOption =
    voiceSelect.selectedOptions[0].getAttribute("data-name");
  for (const voice of voices) {
    if (voice.name === selectedOption) {
      utterThis.voice = voice;
    }
  }

  synth.speak(utterThis);

  utterThis.onerror = (event) => {
    console.log(
      `An error has occurred with the speech synthesis: ${event.error}`,
    );
  };

  inputTxt.blur();
};

规范

规范
Web Speech API
# speechsynthesiserrorevent

浏览器兼容性

另见