1、概述
通过微信小程序wx.startRecord()和wx.stopRecord()接口录音并上传silk录音文件至服务器,通过ffmpeg将silk录音文件转成wav录音文件,再通过百度语音识别 REST API 获取语音识别后的结果。
2、代码实现
录音和语音文件上传
//index.js
//开始录音。当主动调用wx.stopRecord,
//或者录音超过1分钟时自动结束录音,返回录音文件的临时文件路径。
//当用户离开小程序时,此接口无法调用。
wx.startRecord({
success: function (res) {
console.log('录音成功' + JSON.stringify(res));
that.setData({
voiceButtonName: '语音识别',
voicePlayButtonName: '开始播放',
tempFilePath: res.tempFilePath
})
//上传语音文件至服务器
wx.uploadFile({
url: 'https://你的域名/upload',
filePath: res.tempFilePath,
name: 'file',
// header: {}, // 设置请求的 header
formData: {
'msg': 'voice'
}, // HTTP 请求中其他额外的 form data
success: function (res) {
// success
console.log('begin');
console.log(res.data);
var json = JSON.parse(res.data);
console.log(json.msg);
var jsonMsg = JSON.parse(json.msg);
console.log(jsonMsg.result);
wx.navigateTo({
url: '../voicePage/voicePage?voiceData