uni-app微信小程序+Java实现百度语音识别

本文介绍如何使用uni-app开发微信小程序,并结合Java后台实现百度语音识别。首先在百度AI开放平台创建应用获取API Key和Secret Key。在uni-app中录音并上传到服务器,然后通过Java后台调用百度语音识别接口。遇到的问题包括微信小程序uploadFile在真机上的限制,以及上传文件的Content-Type设置。最后提供了Java后台使用Struts2进行文件上传和百度语音识别的代码示例。
摘要由CSDN通过智能技术生成

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);
					}
				}

			});
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值