--- title: SpeechGrammar slug: Web/API/SpeechGrammar tags: - API - SpeechGrammar - Web Speech API - 实验性 - 接口 - 识别 - 语音 - 语音识别 translation_of: Web/API/SpeechGrammar ---
{{APIRef("Web Speech API")}}{{SeeCompatTable}}
Web Speech API 的 SpeechGrammar
接口 表示了语音识别对象服务想要识别的一系列词语或模式。
文法通过 JSpeech Grammar Format (JSGF.) 来定义,其他格式的文法会在以后支持。
SpeechGrammar
对象。SpeechGrammar
对象实例中包含文法的字符串。SpeechGrammar
对象的权重。var 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 ;' var recognition = new SpeechRecognition(); var speechRecognitionList = new SpeechGrammarList(); speechRecognitionList.addFromString(grammar, 1); recognition.grammars = speechRecognitionList; console.log(speechRecognitionList[0].src); // 应该返回和上面语法变量一样的内容 console.log(speechRecognitionList[0].weight); // 应该返回 1 - 与上面第四行所设置的权重一致
规格 | 状态 | 描述 |
---|---|---|
{{SpecName('Web Speech API', '#speechreco-speechgrammar', 'SpeechGrammar')}} | {{Spec2('Web Speech API')}} |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatChrome(33)}} [1] | {{CompatGeckoDesktop(44)}} [2] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatVersionUnknown}}[1] | {{CompatUnknown}} | 2.5 | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
webkitSpeechRecognition
;此外你还需要将你的代码运行在一个 Web 服务器上来保证其正常工作。media.webspeech.recognition.enable
来启用 —— 一旦所需的内部权限解决后,它很快就会启用。如需在应用中使用语音识别,你必须在你的 manifest 中指明下面的权限:
"permissions": { "audio-capture" : { "description" : "Audio capture" }, "speech-recognition" : { "description" : "Speech recognition" } }
若还需要设置应用的特权类型,因此你还需要包含以下这项:
"type": "privileged"