引言:
最近在做ai问答的小程序开发,需要有一个语音输入转文字发送的功能,找了网上很多教程,包括还有问了ai,发现都没有很好的解决,这里给大家一个标准的解决方案
准备
注册账号
首先,需要去腾讯云平台注册账号,开通语音服务
开启之后,需要拿到三个值,appid,secretid,secretkey,因为调腾讯的语音服务是收费的,
开启小程序插件
注册完之后,我们需要在小程序开发平台,开启腾讯语音插件

找到账号设置, 第三方设置,在插件管理里面,搜索腾讯云智能语音,添加
代码引入插件
开启之后,在我们的代码中,找到app.json,在里面添加
"plugins": {
"QCloudAIVoice": {
"version": "2.3.10",
"provider": "wx3e17776051baf153"
}
},
我写这个博客时的最新版本时2.3.10,大家可以根据自己的版本需求更改,但是provider是不变的
代码实现
准备好了这些之后,我们就可以开始实现了,主要的js逻辑代码如下,大家如果配置好了,直接复制过去就可以使用了,要把对应的appid等,换成自己的,start和end方法,需要绑定自己的元素上
/** asr.js **/
// 获取应用实例
const plugin = requirePlugin("QCloudAIVoice");
let resultText = '';
Page({
data: {
socket: null,
speechRecognizerManager: null,
isCanSendData: false,
result: '',
res: '',
recording: false,
disabled: false,
isRecognizeStop: false,
recognizeResult: ''
},
onLoad: function () {
this.speechRecognizerManager = plugin.speechRecognizerManager();
// 开始识别
this.speechRecognizerManager.OnRecognitionStart = (res) => {
console.log('开始识别', res);
this.isCanSendData = true;
this.setData({
recording: true,
disabled: true,
result: '',
});
};
// 一句话开始
this.speechRecognizerManager.OnSentenceBegin = (res) => {
console.log('一句话开始', res);
};
// 识别变化时
this.speechRecognizerManager.OnRecognitionResultChange = (res) => {
console.log('识别变化时', res);
this.setData({
result: `${resultText || ''}${res.result.voice_text_str}`
});
};
// 一句话结束
this.speechRecognizerManager.OnSentenceEnd = (res) => {
console.log('一句话结束', res);
resultText += res.result.voice_text_str;
this.setData({
result: resultText
});
};
// 识别结束
this.speechRecognizerManager.OnRecognitionComplete = (res) => {
console.log('识别结束', res);
this.isRecognizeStop = true;
this.setData({
recording: false,
disabled: false
});
};
// 识别错误
this.speechRecognizerManager.OnError = (res) => {
console.log(res);
this.isCanSendData = false;
this.setData({
recording: false,
disabled: false
});
};
this.speechRecognizerManager.OnRecorderStop = () => {
console.log('超过录音时长');
};
},
startLy: async function (e) {
const self = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success() {
// 用户已经同意小程序使用录音功能,后续调用 record 接口不会弹窗询问
self.startAsr();
}, fail() {
wx.showToast({ title: '未获取录音权限', icon: 'none' });
// console.log("fail auth")
}
});
} else {
self.startAsr();
// console.log("record has been authed")
}
}, fail(res) {
// console.log("fail",res)
}
});
},
startAsr: function () {
wx.showToast({
title: '建立连接中',
icon: 'none'
});
resultText = '';
const params = {
// 用户参数
secretkey: 'secretkey',
secretid: "secretid",
appid: appid,
// 录音参数
// duration: 100000,
// frameSize: 1.28, //单位:k
// 实时识别接口参数
engine_model_type: '16k_zh',
// 以下为非必填参数,可跟据业务自行修改
// hotword_id : '08003a00000000000000000000000000',
needvad: 1,
// filter_dirty: 1,
// filter_modal: 2,
filter_punc: 1,
// convert_num_mode : 1,
word_info: 2,
vad_silence_time: 200
};
this.speechRecognizerManager.start(params);
wx.vibrateShort();
},
endLy: function (e) {
this.setData({
recording: false,
disabled: false
});
this.speechRecognizerManager.stop();
}
});

1927

被折叠的 条评论
为什么被折叠?



