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

瀏覽器相容性

另見