uni-app语音转文字(百度篇)

在这里插入图片描述

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<button type="default" @click="aaa"> 打开相册</button>
			<button @touchstart="startRecord" @touchend="endRecord">开始录音</button>
			<button @tap="playVoice">播放录音</button>
			<button v-if="zwz">右滑语音转文字</button>
		</view>

		<view class="">
			{{txt}}
		</view>
		
		<button class="" @click="fff">
			录视频
		</button>
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	export default {
		data() {
			return {
				voicePath: '',

				options: {}, // 语音转文字的设置

				text: '', // 识别的文字
				txt: '显示的文字', // 
				width: '' ,// 屏幕宽度
				
				zwz:false,
			}
		},
		onLoad() {
			let self = this;
			recorderManager.onStop(function(res) {
				console.log('recorder stop' + JSON.stringify(res));
				self.voicePath = res.tempFilePath;
			});

			uni.getSystemInfo({
				success: (res) => {
					// 屏幕宽度
					this.width = res.screenWidth
					console.log(res.screenWidth);
				}
			})
		},
		methods: {
			// 相册
			aaa() {
				uni.chooseImage({
					success(res) {
						console.log(res.tempFilePaths)
					}
				})
			},
			// 开始录音
			startRecord() {
				console.log('开始录音');
				// 转文字

				recorderManager.start({
					duration: 180000
				});
				this.handleVoice()
			},
			// 录音结束
			endRecord(call) {
				console.log('录音结束');
				recorderManager.stop(call => {
					// 临时音频文件路径
					console.log(call.tempFilePath)
				});

				// 判断是否在右侧
				const endX = call.changedTouches[0].clientX;
				const endY = call.changedTouches[0].clientY;
				console.log(endX, this.width * 0.75)
				// 判断手指是否移入右侧
				if (endX >= (this.width * 0.75)) {
					// 因plus.speech.startRecognize为异步操作,古使用定时器使this.txt得以更新
					setTimeout(() => {
						// console.log(this.text)
						this.txt = this.text
						this.zwz = false
					}, 0)

				}
			},
			
			// 播放录音
			playVoice() {
				console.log('播放录音');

				if (this.voicePath) {
					innerAudioContext.src = this.voicePath;
					innerAudioContext.play();
				}
			},

			// 录音转文字
			handleVoice() {
				console.log('语音输入')
				let _this = this;
				this.options.engine = 'baidu'
				this.options.punctuation = false; // 是否需要标点符号 
				this.options.timeout = 10 * 1000; //超时时间
				this.options.userInterface = false; // 是否显示语音界面
				plus.speech.startRecognize(this.options, (s) => {
					_this.text = s
					_this.zwz = true
					console.log(_this.text)
					plus.speech.stopRecognize(); // 关闭
				});
			},
			
			
			// 视频录制
			fff(){
				uni.chooseVideo({
					// sourceType:['camera'],
					success: (res) => {
					// 临时文件路径
						console.log(res.tempFilePath)
					}
				})
			}
		},
	}
</script>

将本地资源上传到开发者服务器
uni.uploadFile({})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值