SpeechSynthesisErrorEvent:error 屬性
error 屬性是 SpeechSynthesisErrorEvent 介面的一個屬性,它返回一個錯誤程式碼,指示語音合成嘗試中出現了什麼問題。
值
包含錯誤原因的字串。可能的值有:
canceled-
呼叫
SpeechSynthesis.cancel方法導致SpeechSynthesisUtterance在開始朗讀之前就被從佇列中移除。 interrupted-
呼叫
SpeechSynthesis.cancel方法導致SpeechSynthesisUtterance在開始朗讀之後、完成之前被中斷。 audio-busy-
由於使用者代理無法訪問音訊輸出裝置,操作此時無法完成(例如,使用者可能需要透過關閉另一個應用程式來糾正此問題)。
audio-hardware-
由於使用者代理無法識別音訊輸出裝置,操作此時無法完成(例如,使用者可能需要連線揚聲器或配置系統設定)。
network-
由於某些必需的網路通訊失敗,操作此時無法完成。
-
由於沒有可用的合成引擎,操作此時無法完成(例如,使用者可能需要安裝或配置合成引擎)。
synthesis-failed-
由於合成引擎引發了錯誤,操作失敗。
-
在
SpeechSynthesisUtterance.lang中設定的語言沒有可用的合適語音。您可以使用window.speechSynthesis.getVoices()方法來確定使用者瀏覽器支援哪些語音和語言。 -
SpeechSynthesisUtterance.voice中設定的語音不可用。 text-too-long-
SpeechSynthesisUtterance.text屬性的內容太長,無法合成。 invalid-argument-
SpeechSynthesisUtterance.rate、SpeechSynthesisUtterance.pitch或SpeechSynthesisUtterance.volume屬性的內容無效。 not-allowed-
操作的開始未被允許。
示例
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.error(
`An error has occurred with the speech synthesis: ${event.error}`,
);
};
inputTxt.blur();
};
規範
| 規範 |
|---|
| Web Speech API # dom-speechsynthesiserrorevent-error |
瀏覽器相容性
載入中…