uniapp小程序语音转文字功能

先看效果

首先是在微信公众平台),左侧菜单栏中的设置-->第三方设置下的插件管理-->添加-->搜索微信同声传译

如果搜索不到通过这个链接添加https://fuwu.weixin.qq.com/search?tab=3&type=&serviceType=3&page=1&kw=微信同声传译

 接下来打开你的项目

// manifest.json
"mp-weixin": {
    "appid": "xx", // 这是你小程序的AppId
    ...
    "plugins": {
        "WechatSI": {
            "version": "0.3.3", // 版本 可以自己去文档查看选择版本
            "provider": "wx069ba97219f66d99" // 插件id
        }    
    }
}

 做完以上步骤之后,就可以进行开发了,效果图里的业务代码不方便展示,我自己写了个demo可以直接复制粘贴

<template>
	<view>
		<button @touchstart="streamRecord" @touchend="endStreamRecord" form-type="submit" type="primary"
			class="fc-white">语音识别按钮</button>
		<view> 语音识别内容:{{currentText}} </view>

		<!-- 语音音阶动画 长按说话时的动画 -->
		<view class="prompt" v-if="animation">
			<section class="dots-container">
				<view class="dot"></view>
				<view class="dot"></view>
				<view class="dot"></view>
				<view class="dot"></view>
				<view class="dot"></view>
			</section>
			<text>录音中...</text>
		</view>
	</view>
</template>

<script>
	var plugin = requirePlugin("WechatSI")
	let manager = plugin.getRecordRecognitionManager()
	export default {
		data() {
			return {
				currentText: "",
				animation: false,
			}
		},
		methods: {
			streamRecord: function() {
				console.log('开始')
				this.animation = true;
				manager.start({
					lang: 'zh_CN',
				})
			},
			endStreamRecord: function() {
				this.animation = false;
				console.log('结束')
				manager.stop()
			},
			initRecord: function() {
				//有新的识别内容返回,则会调用此事件
				manager.onRecognize = (res) => {
					let text = res.result
					this.currentText = text
				}
				// 识别结束事件
				manager.onStop = (res) => {
					console.log(res, 37);
					let text = res.result
					if (text == '') {
						console.log('没有说话')
						return
					}
					this.currentText = text
				}
			},
		},
		onLoad() {
			this.initRecord()
		},

	}
</script>

<style lang="scss" scoped>
	/* 动画 */
	.prompt {
		width: 100%;
		height: 160rpx;
		position: fixed;
		bottom: 50vh;
	}

	.prompt text {
		position: absolute;
		bottom: 2px;
		color: white;
		left: calc(45%);
		animation: puls 1.5s infinite ease-in-out;
	}

	.dots-container {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 80px;
		width: 45%;
		position: absolute;
		bottom: 0px;
		left: calc(27.5%);
		background-color: rgba(0, 0, 0, 0.5);
		border-radius: 40rpx;
	}

	.dot {
		height: 28rpx;
		width: 28rpx;
		margin-right: 20rpx;
		border-radius: 20rpx;
		background-image: linear-gradient(#5396FF, #AEDAFF);
		animation: pulse 1.5s infinite ease-in-out;
	}

	.dot:last-child {
		margin-right: 0;
	}

	.dot:nth-child(1) {
		animation-delay: -0.3s;
	}

	.dot:nth-child(2) {
		animation-delay: -0.1s;
	}

	.dot:nth-child(3) {
		animation-delay: 0.1s;
	}

	@keyframes pulse {
		0% {
			transform: scale(0.8);
			background-color: #66A3FF;
			/* 更改为与.dot背景色相近的颜色 */
			box-shadow: 0 0 0 0 rgba(102, 163, 255, 0.7);
			/* 使用相同的颜色 */
		}

		50% {
			transform: scale(1.2);
			background-color: #ADD8FF;
			/* 稍浅的颜色,增加对比度 */
			box-shadow: 0 0 0 10px rgba(174, 218, 255, 0);
			/* 使用.dot的结束颜色,但透明度为0 */
		}

		100% {
			transform: scale(0.8);
			background-color: #66A3FF;
			/* 与0%时的颜色相同 */
			box-shadow: 0 0 0 0 rgba(102, 163, 255, 0.7);
			/* 与0%时的box-shadow相同 */
		}
	}

	@keyframes puls {
		0% {
			transform: translateY(0px)
		}

		50% {
			transform: translateY(-4px)
		}

		100% {
			transform: translateY(0px)
		}
	}
</style>

在使用uni-app开发微信小程序时,与直接使用微信网页开发工具开发微信小程序有一些差别。由于uni-app可以开发多平台,因此不同平台的开发需要在指定的位置进行相应的配置才能生效。对于引入微信同声传译小程序,有两种方式可以实现。一种是整个小程序可使用,即小程序中的所有分包都可以使用该功能。另一种是指定对应的分包可使用该功能。在使用插件之前,需要在manifest.json文件的mp-weixin内声明使用的插件,并进行相应的配置。具体的配置可以参考所使用插件的开发文档。在代码中,可以直接使用文档中提供的代码来实现微信同声传译的功能。通过引入插件并调用相应的方法,可以实现文本语音功能。例如,可以使用plugin.textToSpeech方法来实现将指定的文本换为语音功能。在方法的参数中,可以指定语言、是否启用语音合成等。成功调用该方法后,可以在回调函数中获取到生成的语音文件的信息。 #### 引用[.reference_title] - *1* *2* *3* [uni-app微信小程序开发,引入微信同声传译插件](https://blog.csdn.net/qq_32837111/article/details/106804236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值