微信小程序语音实时转文字
注意:此文档是基于 uniapp 开发微信小程序的总结
微信小程序实现语音实时转文字功能,引入微信小程序自研的同声传译插件(官方文档:插件服务 / 微信同声传译)
引入
1. 小程序后台
微信小程序后台-> 设置->第三方设置-> 插件管理(添加插件)-> 搜索微信同声传译

2. maneifest.json
下面一段代码添加到mp-weixin,version,provider在插件管理中可以看到具体信息
▼json复制代码"plugins": { "WechatSI": { "version": "0.3.6", "provider": "wx069ba97219f66d99" } }
完整流程图

实现流程
1. 核心界面实现

▼html复制代码<view class="input-container"> <image class="record-icon" :class="{ 'recording': isRecording }" src="/static/icon/record.png" mode="aspectFit" @click="toggleRecording" ></image> <input class="message-input" v-model="inputMessage" :placeholder="inputPlaceholder" :disabled="isLoading" @confirm="sendMessage" confirm-type="send" /> <button class="send-btn" :class="{ 'send-btn-disabled': !inputMessage.trim() || isLoading }" @click="sendMessage" :disabled="!inputMessage.trim() || isLoading" > <text v-if="!isLoading">发送</text> <text v-else>...</text> </button> </view>
2. 变量添加
▼js复制代码// 录音相关 isRecording: false, // 是否正在录音 plugin: null, // 微信同声传译插件实例 manager: null // 录音管理器
3. 页面加载
▼js复制代码onLoad(options) { // 初始化微信同声传译插件 this.initVoicePlugin() }, /** * 初始化微信同声传译插件 */ initVoicePlugin() { try { // 引入微信同声传译插件 this.plugin = requirePlugin("WechatSI") // 获取录音管理器 this.manager = this.plugin.getRecordRecognitionManager() // 监听识别开始事件 this.manager.onStart = (res) => { console.log('录音识别开始', res) this.isRecording = true } // 监听识别结果事件(实时返回) this.manager.onRecognize = (res) => { console.log('实时识别结果:', res) if (res.result) { // 实时更新到输入框 this.inputMessage = res.result } } // 监听识别结束事件 this.manager.onStop = (res) => { console.log('录音识别结束', res) this.isRecording = false if (res.result) { // 最终结果更新到输入框 this.inputMessage = res.result } } // 监听识别错误事件 this.manager.onError = (res) => { console.error('录音识别错误:', res) this.isRecording = false uni.showToast({ title: '识别失败,请重试', icon: 'none', duration: 2000 }) } } catch (error) { console.error('微信同声传译插件初始化失败:', error) uni.showToast({ title: '语音功能不可用', icon: 'none', duration: 2000 }) } console.warn('当前环境不支持微信同声传译插件') },
4. 用户点击录音按钮
▼js复制代码/** * 切换录音状态 */ toggleRecording() { if (!this.manager) { uni.showToast({ title: '语音功能未初始化', icon: 'none', duration: 2000 }) return } if (this.isRecording) { // 停止录音 this.stopRecording() } else { // 开始录音 this.startRecording() } uni.showToast({ title: '仅支持微信小程序', icon: 'none', duration: 2000 }) }, /** * 开始录音 */ startRecording() { try { // 先清空输入框 this.inputMessage = '' // 开始录音识别 this.manager.start({ duration: 60000, // 最长录音时长(毫秒) lang: 'zh_CN' // 语言:中文 }) uni.showToast({ title: '开始录音...', icon: 'none', duration: 1000 }) } catch (error) { console.error('开始录音失败:', error) this.isRecording = false uni.showToast({ title: '录音失败,请重试', icon: 'none', duration: 2000 }) } }, /** * 停止录音 */ stopRecording() { try { this.manager.stop() uni.showToast({ title: '录音结束', icon: 'none', duration: 1000 }) } catch (error) { console.error('停止录音失败:', error) this.isRecording = false } },
5. 切换页面
▼js复制代码onUnload() { // 清理录音资源 if (this.manager) { this.manager.stop() } },
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
作者分享
今日完成
1. 单词
1. bump exotic(4次)、mad fax fell grope convict(2次) brick irrigate penetrate inventory uproar(4次)、 haste(4次)、slippery augment detective doze
2. hound plug compile mistake exception weird rid vulgar(5次)、poetry reed ozone ballot
3. abide herb tumble wonder colonel yard superiority assemble panorama coalition(2次)
4. composite concert magistrate(3次)、potray leather criterion central pinch(4次)premium(2次)、civilian strife(2次) butcher
5. superior motivate shrug pilgrim vacuum summit drastic(3次)、testimony bewilder(3次)、syndrome weed negligible(2次) twist polar
6. paste artery bolt triumph ruthless(2次)、temperament(2次)、 wicked(2次)、criminal
2. 线代1000题第4讲题目+订正
3. 2007英一阅读text4+订正
4. 长难句订正
5. 计算机网络王道1.2习题
6. 单词回顾
7. 软件测试2个实验文档完成
2
6月24日 星期三
---
✅ 今日完成
1. 单词
1. qwartz(2次)、conjunction (2次)、constitution blur pathetic(3次)
2. zinc (2次)、senator pave lack spur(2次)ethnic(3次)entitle savage inward、equation
3. eminent(4次)、tilt clockwise distract rejoice pine shipment trademark snatch(4次)、swamp(2次)、 disc、discharge
4. murder damp aspire(4次)、prone notify motel rarely compulsory stir owl
5. grace miniature namely linger(2次)、pamphlet、wrinkle、rust
6. retort gallop(2次)、piston(2次)、bagde revenue gulf sand critic
2. 线代30讲第三讲习题完成+订正
3. 07年英语一阅读text3
4. 长难句
5. 计网1.1习题(17道)+订正
6. 晚上单词回顾
7. 计网1.1订正
8. 计网第二章视频学习
⏰ 明天计划:
📚 今日感悟:
早上单词学习、线代30讲完成
下午
完成07年英语一阅读text3,错了2个,感觉其中第一题只能用排除法做
单词学习:parachute、ruin
线代今天看30讲第四讲例题(没几个),再加上完成对应的习题和订正,习题总共10道,但自己用了4h,是不是效率低下的原因
晚上
完成每日长难句,今日长难句比较简单
计网1.1习题(17道)+订正
信道利用率、数据报、时延公式、计算机网络功能,对于计算时延题目,不要过于依赖公式,画图才是正解
单词回顾
计网1.1订正
计网第二章视频学习
今日花了588报了6个月的数学答疑,个人感觉蛮值的
3
6月23日 星期二
---
✅ 今日完成
1. 单词
1. trolley adjoin moist pedestrain elapse furnish glorious tear cabbage approval (2次)exploit turbulent(3次) geometry esteem protype(2次) glory
2. chalk fleet hardship envisage spectacle(3次)partial(2次) confine inhabitant prize glow album cassette radio leak(2次)
3. proportion cough several integrity editorial strech harness tribute(3次) episode(2次)、climb tease (2次)、praise revelation verse
4. remainder edition refuge bin primitive glare stroll imperial peasant slim discriminate robe(3次)、strain occassion
5. adapt bind prose ascertain cunning
2. 高数1000题第三讲订正
3. 计组第二章对应习题完成及订正
4. 计网就看了几节视频
⏰ 明天计划:
📚 今日感悟:
早上单词、高数1000题第三讲订正
下午计组第二章对应习题完成及订正
晚上单词回顾、计网就看了几节视频、软件测试就完成了一点
总时延 n个分组的发送时延+(m-1)*1个分组的发送时延+m*1段链路的传播时延
发现高数1000题第三讲电子版有许多和原题对不上
极限存在才可以拆成两部分极限相加
大小端模式大同小异口诀非常好用
非规格数特点阶码全为0尾数不为0
今天不知道为什么许多软件突然被删了,又得重新下回
晚上就不应该回寝室学,室友太吵了,今天还来了个隔壁寝室的人过来聊天,明天图书馆待到晚上8点吧
3
6月21日 星期日
✅ 今日完成
1. 单词commentary qualify plausible definite dazzle hamper particle settlement impair implicit hinder accumulate organ toast verdict rarely react rational perish enlighten vessel sensitive
2. 高数30讲第三讲完成+订正
3. 线代1000题第三讲完成+订正
4. 计算机组成原理王道习题2.2
5. 计算机网络湖科大第一、二章视频学习
⏰ 明天计划:
📚 今日感悟:
早上背单词、高数30讲完成+订正
下午线代1000题第三讲完成+订正,对于线性相关的概念又加深了
晚上计算机组成原理王道习题2.2、计算机网络湖科大第一、二章视频学习
向量组部分与整体的关系结论十分有用,部分相关能推整体相关,整体相关推部分无关
3
6月20日 星期六
---
✅ 今日完成
1. 单词 pertain tread formidable admire vicious administration guild commercial commodity breakdown lateral ceremomy deduct venture benevolent defy advisable deter affiliate commission devise affection implement corporate fling beam physician phase
2. 30讲线代第三讲订正
3. 计算机组成原理第二章湖科大视频
4. 计算机网络第一章部分
⏰ 明天计划:
📚 今日感悟:
早上单词、30讲线代第三讲订正
下午计算机组成原理视频
晚上计算机网络视频
计算机组成原理第二章湖科大视频看得我心态有点崩,感觉自己有点焦虑了, 因为我看408的内容还有好多
3
