1.准备工作
由于微信小程序和百度语音识别的记录博客少之又少,所以这篇博客就诞生了。
注册登录百度AI开放平台(http://ai.baidu.com/)
找到语音技术 - 应用列表,创建应用,填写相应信息,语音包名选择不需要
创建完成后可以找到:应用列表点击查看App ID、API Key、Secret Key
注意要领取免费赠送次数,有15万次的短语音识别,我已经用掉94次,不用白不用
2.思路和uni-app开发
1.思路
使用uni-app官方代码,经测试得出经过录音会在小程序上出现临时文件,我们就把这临时文件上传到服务器上供百度语音识别,然后把识别的内容返回到小程序前端展示。
2.uni-app端开发
<template>
<view>
<view style="padding: 40% 5%;font-size: 40rpx;text-align: center;margin-bottom: 5%;">
<text>以下是识别内容:</text>
</view>
<view style="font-size: 40rpx;text-align: center;">
<text>{
{
sbinfo}}</text>
</view>
<view style="padding: 50% 5%;">
<button @touchstart="gtouchstart()" @touchend="gtouchend()" style="background-color: #00CD98;color: #fff;">请长按语音识别</button>
</view>
</view>
</template>
<script>
const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
export default {
data() {
return {
voicePath: '',
sbinfo: '',
timeOutEvent: 0, //计时器
}
},
onLoad() {
let self = this;
recorderManager.onStop(function(res) {
console.log("录音文件属性");
console.log(res);
if (res.duration > 60000) {
uni.showToast({
icon: 'none',
position: 'bottom',
title: '说话时间过长,请控制在60秒内'
});
} else {
self.voicePath = res.tempFilePath;
if (self.voicePath != null || self.voicePath != '') {
self.uploadRecord(self.voicePath);
}
}
});
}