SpeechRecognition:result 事件

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

result 事件是 Web Speech API 的一部分,當語音識別服務返回一個結果時觸發——即一個詞語或短語被正確識別並傳達回應用程式。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("result", (event) => { })

onresult = (event) => { }

事件型別

一個 SpeechRecognitionEvent。繼承自 Event

Event SpeechRecognitionEvent

事件屬性

除了下面列出的屬性之外,父介面 Event 的屬性也可使用。

SpeechRecognitionEvent.emma 只讀

返回結果的 Extensible MultiModal Annotation markup language (EMMA) — XML — 表示。

SpeechRecognitionEvent.interpretation 只讀

返回使用者所說內容的語義含義。

SpeechRecognitionEvent.resultIndex 只讀

返回 SpeechRecognitionResultList "陣列" 中已實際發生更改的最低索引值結果。

SpeechRecognitionEvent.results 只讀

返回一個 SpeechRecognitionResultList 物件,代表當前會話的所有語音識別結果。

示例

此程式碼摘自我們的 語音變色器 示例。

您可以在 addEventListener 方法中使用 result 事件。

js
const recognition = new SpeechRecognition();

recognition.addEventListener("result", (event) => {
  const color = event.results[0][0].transcript;
  diagnostic.textContent = `Result received: ${color}.`;
  bg.style.backgroundColor = color;
});

或者使用 onresult 事件處理程式屬性。

js
recognition.onresult = (event) => {
  const color = event.results[0][0].transcript;
  diagnostic.textContent = `Result received: ${color}.`;
  bg.style.backgroundColor = color;
};

規範

規範
Web Speech API
# eventdef-speechrecognition-result
Web Speech API
# dom-speechrecognition-onresult

瀏覽器相容性

另見