语音识别:maxAlternatives 属性

maxAlternativesSpeechRecognition 接口的一个属性,它设置每个 SpeechRecognitionResult 提供的 SpeechRecognitionAlternative 的最大数量。

默认值为 1。

一个数字,表示每个结果返回的最大备选方案数量。

示例

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

js
const grammar =
  "#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;";
const recognition = new SpeechRecognition();
const speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
//recognition.continuous = false;
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.maxAlternatives = 1;

// ...

规范

规范
Web 语音 API
# dom-speechrecognition-maxalternatives

浏览器兼容性

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

另请参阅