使用讯飞语音识别----前后端如何交互?

本文讲述了作者在使用Uniapp开发微信小程序时,遇到音频文件无法被讯飞语音识别的问题,通过后端使用ffmpeg进行音频格式转换以解决问题的过程。前端负责录音上传,后端处理音频转换和识别请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端采用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: {
					
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值