HTML5之語音識別實例
代碼
<input type="text" x-webkit-speech id="d1" lang="zh-CN" x-webkit-grammar="bUIltin:search" onwebkitspeechchange="foo()"/>
<script>
function foo(){
var n = document.getElementById("d1").value;
if(n == "百度"){
window.location.href = "http://www.baidu.com";
}else{
window.location.href = "http://www.ahsdxy.ah.edu.cn/";
}
}
</script>
說明:
1)x-webkit-speech:語音識別支持屬性
<input type="text" x-webkit-speech/>
2)lang:設置語言種類,比如漢
語:lang="ch-CN"
<input type="text" x-webkit-speech lang="ch-CN"/>
3)
x-webkit-grammar :語音輸入語法
比如: x-webkit-grammar=
"bUIltin:search"使得語音輸入的內容盡量靠近搜索內容,去除多余的字符,例如“的、啦”等
<input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search"/>
4) onwebkitspeechchange :語音輸入事件,當語音改變時觸發
比如:onwebkitspeechchange="foo()" ,當停止語音時,會觸發js中的foo()函數
<input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search"
onwebkitspeechchange="foo()"/>
此時,需要寫相應的JavaScript函數foo()
<script>
function foo(){
//函數體,如下:
alert(8);
}
</script>