SpeechRecognitionAlternative

SpeechRecognitionAlternativeWeb 语音 API 的一个接口,表示语音识别服务识别出的单个单词。

实例属性

SpeechRecognitionAlternative.transcript 只读

返回包含识别单词的文本的字符串。

SpeechRecognitionAlternative.confidence 只读

返回一个介于 0 和 1 之间的数值估计,表示语音识别系统对识别结果的置信度。

示例

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

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-alternative

浏览器兼容性

BCD 表仅在浏览器中加载

另请参阅