一、语音识别服务申请
在manifest.json开启Speech语音输入权限,如下图
Id和密钥需去百度智能云官网进行注册应用,并购买语音识别服务(第一次使用可免费领取限量的资源)
二、使用
1、点击开始录音
// #ifdef APP-PLUS
plus.speech.startRecognize({
engine: 'baidu',
lang: 'zh-cn',
'userInterface': false,
'continue': true
});
// #endif
2、点击停止录音
// #ifdef APP-PLUS
plus.speech.stopRecognize();
// #endif
3、监听语音识别
// 在onLoad中编写
// #ifdef APP-PLUS
plus.speech.addEventListener('start', this.ontStart, false);
plus.speech.addEventListener('volumeChange', this.onVolumeChange, false);
plus.speech.addEventListener('recognizing', this.onRecognizing, false);
plus.speech.addEventListener('recognition', this.onRecognition, false);
plus.speech.addEventListener('end', this.onEnd, false);
// #endif
4、监听方法
ontStart() {
this.title = '开始录音';
this.text = '';
},
onVolumeChange(e) {
console.log(e.volume);
},
onRecognizing(e) {
this.partialResult = e.partialResult;
console.log(e.partialResult);
},
onRecognition(e) {
this.text += e.result;
this.text?(this.text+='\n'):this.text='';
this.result = this.text;
this.partialResult = e.result;
console.log(e.result);
},
onEnd() {
// 结束录音
if(!this.text||this.text==''){
plus.nativeUI.toast('没有识别到内容');
}
this.result = this.text;
this.title = '未开始';
this.partialResult = '';
},
以上步骤详见uniapp官方文档
提示:该功能只能使用自定义基座或云端包才能生效