概要
uniapp-接入百度语音识别文字,匹配相应指令进行下一步操作;
1.注册百度云获取AppId、API Key和Secret Key值;
2.在hbuider中App模块配置中选择百度语音识别,如图:
注册百度云获取AppId、API Key和Secret Key值
百度云官网:https://login.bce.baidu.com/?redirect=https%3A%2F%2Fconsole.bce.baidu.com%2Fai%2F%3F_%3D1719307152584%26fromai%3D1#/ai/speech/app/detail~appId=5346349
进入官网注册后获取AppId、API Key和Secret Key;
注意: 首次注册可获取免费的语音识别的调试次数,或者购买,如没有领取也未曾购买会出现4004报错
在hbuider中配置
获取到相应的值之后在hbuider中配置,如图:
运行视图
<block slot="right">
<view style="width: 100%;text-align: end;" @touchstart="startRecognize" @touchend="endRecognize">
<uni-icons type="mic" color="#fff" size="30"></uni-icons>
</view>
</block>
初始化代码块
onLoad() {
// #ifdef APP-PLUS
// 监听语音识别事件
plus.speech.addEventListener('start', this.ontStart, false);
plus.speech.addEventListener('recognition', this.onRecognition, false);
plus.speech.addEventListener('end', this.onEnd, false);
// #endif
},
方法代码块
ontStart() {
this.text = '';
// console.log('Event: 开始识别');
},
onRecognition(e) {
this.text += e.result;
// console.log('Event: 识别例句,内容'+e.result);
},
onEnd() {
if(!this.text||this.text==''){
plus.nativeUI.toast('没有识别到内容');
}
},
startRecognize() {
// console.log('startRecognize 开始识别');
// #ifdef APP-PLUS
plus.speech.startRecognize({
engine: 'baidu'
});
// #endif
},
endRecognize() {
// console.log('endRecognize 结束识别');
// #ifdef APP-PLUS
plus.speech.stopRecognize();
// #endif
}