{{APIRef(“Web Speech API”)}} {{SeeCompatTable}}
在SpeechRecognition该界面的Web语音API是用于识别服务的控制器接口; 这也处理从识别服务发送的{{domxref(“SpeechRecognitionEvent”)}}。
注意:在Chrome上,在网页上使用语音识别涉及基于服务器的识别引擎。您的音频将发送到Web服务进行识别处理,因此无法脱机工作。
SpeechRecognition对象。SpeechRecognition 还从其父接口{{domxref(“EventTarget”)}}继承属性。
SpeechRecognition。SpeechRecognition。如果未指定,则默认为HTML {{htmlattrxref(“lang”,“html”)}}属性值,或者如果未设置,则默认为用户代理的语言设置。false。)true)或不返回(false。)中间结果是尚未最终的结果(例如{{domxref(“SpeechRecognitionResult.isFinal”)}}属性false。)SpeechRecognition用于处理实际识别的语音识别服务的位置。默认值是用户代理的默认语音服务。SpeechRecognition。SpeechRecognition 还从其父接口{{domxref(“EventTarget”)}}继承方法。
SpeechRecognition。在我们简单的语音颜色转换器示例中,我们SpeechRecognition使用{{domxref(“SpeechRecognition.SpeechRecognition”,“SpeechRecognition()”)}}构造函数创建一个新的对象实例,创建一个新的{{domxref(“SpeechGrammarList”)}},并将其设置为SpeechRecognition使用{{domxref(“SpeechRecognition.grammars”)}}属性的实例识别的语法。
在定义了一些其他值之后,我们设置它以便在发生单击事件时启动识别服务(请参阅{{domxref(“SpeechRecognition.start()”)}}。)当成功识别结果时, {{domxref(“SpeechRecognition.onresult”)}}处理程序触发,我们提取从事件对象中说出的颜色,然后将{{htmlelement(“html”)}}元素的背景颜色设置为该颜色。
var grammar ='#JSGF V1.0; 语法颜色; public <color> = aqua | azure | 米色| 浓汤| 黑色| 蓝色| 棕色| 巧克力| 珊瑚| 深红色| 青色| 紫红色| ghostwhite | 金| 一枝黄花| 灰色| 绿色| 靛蓝| 象牙| 卡其色| 薰衣草| 石灰| 亚麻| 洋红色| 栗色| 莫卡辛| 海军| 橄榄油| 橙色| 兰花| 秘鲁| 粉红色| 李子| 紫色| 红色| 三文鱼| sienna | 银| 雪| 晒黑| 蓝绿色| 蓟| 番茄| 绿松石| 紫罗兰| 白色| 黄色 ;'
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar,1);
recognition.grammars = speechRecognitionList;
//recognition.continuous = false;
recognition.lang ='en-US';
recognition.interimResults = false;
recognition.max替代品= 1;
var diagnostic = document.querySelector('。output');
var bg = document.querySelector('html');
document.body.onclick = function(){
recognition.start();
console.log('准备接收颜色命令。');
}
recognition.onresult = function(event){
var color = event.results [0] [0] .transcript;
diagnostic.textContent ='收到的结果:'+ color;
bg.style.backgroundColor = color;
} | 规格 | 状态 | 评论 |
|---|---|---|
| {{SpecName('Web Speech API','#speechreco-section','SpeechRecognition')}} | {{Spec2('Web Speech API')}} |
{{COMPAT( “api.SpeechRecognition”)}}
要在应用中使用语音识别,您需要在清单中指定以下权限:
“权限”:{
“audio-capture”:{
“description”:“音频捕捉”
},
“语音识别” : {
“description”:“语音识别”
}
} 您还需要一个特权应用,因此您还需要包含此应用:
“类型”:“特权”
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition