SpeechRecognitionEvent: results 属性

resultsSpeechRecognitionEvent 接口的只读属性,它返回一个 SpeechRecognitionResultList 对象,表示当前会话的所有语音识别结果。

具体来说,此对象将包含已返回的所有最终结果,以及所有临时结果的当前最佳假设。当后续 结果 事件触发时,临时结果可能会被更新的临时结果或最终结果覆盖 - 它们甚至可能会被删除,如果它们位于“结果”数组的末尾并且数组长度减小。另一方面,最终结果不会被覆盖或删除。

示例

此代码摘录自我们的 语音颜色更改器 示例。

js
recognition.onresult = (event) => {
  // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
  // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
  // It has a getter so it can be accessed like an array
  // The first [0] returns the SpeechRecognitionResult at position 0.
  // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
  // These also have getters so they can be accessed like arrays.
  // The second [0] returns the SpeechRecognitionAlternative at position 0.
  // We then return the transcript property of the SpeechRecognitionAlternative object
  const color = event.results[0][0].transcript;
  diagnostic.textContent = `Result received: ${color}.`;
  bg.style.backgroundColor = color;
};

规范

规范
Web 语音 API
# dom-speechrecognitionevent-results

浏览器兼容性

BCD 表仅在浏览器中加载

另请参阅