SpeechRecognitionAlternative:transcript 属性

transcriptSpeechRecognitionResult 接口的只读属性,它返回一个包含识别到的单词(或词组)转录文本的字符串。

对于连续识别,会在必要时包含前导或尾随空格,以便连续的 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
# dom-speechrecognitionalternative-transcript

浏览器兼容性

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

另请参阅