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

《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({})

    原文作者:WJLSH123
    原文地址: https://blog.csdn.net/WJLSH123/article/details/123033336
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞