小程序实现语音识别功能,通过语音的方式代替手动输入查询。经过查询微信小程序api,发现微信并没有对外提供语音识别的api,所以要另外想办法。经过多发查找资料发现了思路。
解决思路:
微信小程序提供了录音的功能,通过录音的方式,然后把录音文件传到服务器,后台服务器将语音转码,然后再调用第三方语音识别api,我这里使用的是百度的api,最后在将识别的文字返回给微信小程序。
直接上代码:
小程序端代码:
startRecord: function() {
if (this.recorderManager == null) {
this.recorderManager = wx.getRecorderManager();
this.options = {
duration: 10000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 64000,
format: 'mp3',
frameSize: 50
}
}
this.recorderManager.start(this.options);
this.recorderManager.onStop((res) => {
console.log(res)
wx.uploadFile({
url: 'https://xxxx',//将录音文件传到后台服务器
filePath: res.tempFilePath,
method:'POST',
na