SpeechRecognitionResultList

SpeechRecognitionResultListWeb 语音 API 的一个接口,它表示一个 SpeechRecognitionResult 对象列表,或者如果结果在 非连续 模式下捕获,则表示单个对象。

实例属性

SpeechRecognitionResultList.length 只读

返回“数组”的长度——列表中 SpeechRecognitionResult 对象的数量。

实例方法

SpeechRecognitionResultList.item

一个标准的 getter,允许通过数组语法访问列表中的 SpeechRecognitionResult 对象。

示例

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

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
# speechreco-resultlist

浏览器兼容性

BCD 表格仅在启用了 JavaScript 的浏览器中加载。

另请参阅