这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
SpeechGrammar
是 SpeechGrammar
接口的构造函数,创建一个新的 SpeechGrammar
对象实例。
语法
var mySpeechGrammar = new SpeechGrammar();
Parameters
无。
样例
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;
var newGrammar = new SpeechGrammar();
newGrammar.src = '#JSGF V1.0; grammar names; public <name> = chris | kirsty | mike;'
speechRecognitionList[1] = newGrammar; // 将 SpeechGrammar 对象添加到列表中
规格
规格 | 状态 | 描述 |
---|---|---|
Web Speech API SpeechGrammar() |
Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 33 [1] | 44 (44) [2] | 未实现 | 未实现 | 未实现 |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | (Yes)[1] | ? | 2.5 | 未实现 | 未实现 | 未实现 |
- [1] 语音识别接口目前在 Chrome 中是带前缀的,因此你需要使用带有前缀的名称,例如
webkitSpeechRecognition
;此外你还需要将你的代码运行在一个 Web 服务器上来保证其正常工作。 - [2] 虽然当前的语音识别在桌面版的 Firefox 中不会正常工作,但还是可以在 about:config中,可以通过
media.webspeech.recognition.enable
来启用 —— 一旦所需的内部权限解决后,它很快就会启用。
Firefox OS 权限
如需在应用中使用语音识别,你必须在你的 manifest 中指明下面的权限:
"permissions": {
"audio-capture" : {
"description" : "Audio capture"
},
"speech-recognition" : {
"description" : "Speech recognition"
}
}
若还需要设置应用的特权类型,因此你还需要包含以下这项:
"type": "privileged"