语音识别:grammars 属性

grammars 属性是 SpeechRecognition 接口的一个属性,它返回并设置一个 SpeechGrammar 对象的集合,这些对象代表当前 SpeechRecognition 将理解的语法。

包含代表您应用程序语法的 SpeechGrammar 对象的 SpeechGrammarList

示例

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

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

浏览器兼容性

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

另请参阅