前端采用Uniapp,后端采用Springboot
语音识别采用讯飞的短语音识别,使用个人开发可以获得免费试用期。
本人踩坑经历:使用的uniapp转微信小程序,录音之后的音频文件死活不能通过讯飞的识别,于是我在网上寻找到了使用ffmpeg进行格式化音频文件,使其能够被识别,这一部分我是在后端完成的,前端只负责将语音进上传。FFmpeg使用的命令为:
ffmpeg -y -i 源文件 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 生成文件
前端(核心在于options的设置,可以选择多种,但是我最初使用的是pcm,在本地运行是没问题,一到真机就会出问题,于是就改回mp3,在后端使用ffmpeg进行强装):
// 按住录音事件
longpressBtn() {
this.longPress = '2';
this.countdown(60); // 倒计时
clearInterval(init) // 清除定时器
recorderManager.onStop((res) => {
this.tempFilePath = res.tempFilePath;
this.recordingTimer(this.time);
})
const options = {
sampleRate: 16000, //采样率,有效值 8000/16000/44100
numberOfChannels: 1, //录音通道数,有效值 1/2
encodeBitRate: 96000, //编码码率
format: 'mp3',
}
this.recordingTimer();
recorderManager.start(options);
// 监听音频开始事件
recorderManager.onStart((res) => {})
},
// 松开录音事件
async touchendBtn() {
this.longPress = '1';
await recorderManager.onStop((res) => {
this.tempFilePath = res.tempFilePath
console.log(this.tempFilePath)
this.uploadVoice(this.tempFilePath)
})
this.recordingTimer(this.time)
recorderManager.stop()
},
// 上传
uploadVoice(tempFilePath) {
const token = getToken()
if (tempFilePath != '') {
uni.uploadFile({
url: this.endSideUrl1 +"/chat/voice", // 你的服务器上传接口地址
header: {