HTML5 Web Speech API 语音识别语音合成

HTML5 Web Speech API 支持语音识别和语音合成

1:webkitSpeechRecognition(语音识别)

目前webkitSpeechRecognition API支持情况不是很好。下面是chrome的例子

let newRecognition = new webkitSpeechRecognition();
// 配置是识别到声音就关闭,还是一直识别
newRecognition.continuous = true;
newRecognition.start();// 开启录音
newRecognition.stop();// 关闭录音
// 当成功接收到一段可以识别的语音时触发,所以可能触发多次
newRecognition.onresult = function(event) {
    console.log(event);
}
newRecognition.onsoundstart = (e)=>{
    console.log("开始收听了");
    console.log(e);
}
newRecognition.onspeechstart = (e)=>{
    console.log("开始讲话了");
    console.log(e);
}
newRecognition.onspeechend = (e)=>{
    console.log("讲话完毕");
    console.log(e);
}
newRecognition.onerror = (e)=>{
    console.log("发生异常");
    console.log(e);
}

2:SpeechSynthesisUtterance(语音合成)

var ssu = new window.SpeechSynthesisUtterance('Hello');
window.speechSynthesis.speak(ssu);

var ssu2 = new window.SpeechSynthesisUtterance();
ssu2.text = '你好';
ssu2.lang = 'zh-cn';
ssu2.volume = 1;// 音量0-1
ssu2.rate = 1;// 语速0.1-10
ssu2.pitch = 1;// 音高0-2
ssu2.voiceURI = '';// 指定希望使用的声音

ssu2.onstart = (e)=>{console.log("开始。。。");console.log(e);}
ssu2.onpause = (e)=>{console.log("暂停。。。");console.log(e);}
ssu2.onresume = (e)=>{console.log("重新开始。。。");console.log(e);}
ssu2.onend = (e)=>{console.log("结束。。。");console.log(e);}

window.speechSynthesis.pause();// 暂停
window.speechSynthesis.resume();// 重新开始
window.speechSynthesis.stop();// 停止
window.speechSynthesis.getVoices();// 返回支持的语音包数据

 

你可能感兴趣的:(HTML,JS,html5)