本文原文:/m/?p=383
官方插件上线
微信小程序“同声传译”插件介绍 传送门
遥知之改版
原来使用的是我自己封装的API,现在改成用同传插件,很方便,而且响应速度还快,欢迎切换,给我的服务器减减肥。:)
我只用到了同传插件的语音识别和语音合成功能,没有用翻译能力,感兴趣的小伙伴自己去研究一下。
调用方式
语音识别部分见: page/asr/asr.js
var pageSelf = undefined;var plugin = requirePlugin("WechatSI")let manager = plugin.getRecordRecognitionManager()manager.onRecognize = function (res) {UTIL.log("current result", res.result)}manager.onStop = function (res) {UTIL.log("record file path", res.tempFilePath)UTIL.log("result", res.result)//res.result is the asr result, change the follow step to your source//NLI.process(res.result, pageSelf);}manager.onError = function (res) {UTIL.log("error msg", res.msg)}// 以下是调用同声传译插件的方式//手指按下touchdown_plugin: function () {var _this = thisUTIL.stopTTS();manager.start({ duration: 30000, lang: "zh_CN" })},//手指松开touchup_plugin: function () {manager.stop();wx.showToast({title: '正在识别……',icon: 'loading',duration: 2000})},
page/asr/asr.wxml
<button type="primary" style="border-radius: 20px;" class="btn-style" bindtouchstart="touchdown_plugin" bindtouchend="touchup_plugin">按下录音,松开结束</button>
语音合成(播报)部分见: util/util.js
var plugin = requirePlugin("WechatSI")var innerAudioContext = wx.createInnerAudioContext();innerAudioContext.onError((res) => {// 播放音频失败的回调})function playTTS(text) {//need to add WXAPP plug-in unit: WechatSIplugin.textToSpeech({lang: "zh_CN",tts: true,content: text,success: function (res) {log("succ tts", res.filename)innerAudioContext.src = res.filename;innerAudioContext.play()},fail: function (res) {log("fail tts", res)}})}function stopTTS() {innerAudioContext.stop();}module.exports = {playTTS: playTTS,stopTTS: stopTTS,}