使用微信同声传译插件开发一款翻译类的小程序

使用微信同声传译插件开发一款翻译类的小程序

1. 微信同声传译插件简介

1.1 微信同声传译插件有三个功能:语音输入、文本翻译、语音合成。可参考微信开发文档https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/extended/translator.html

2. 示例代码

2.1 使用语音输入功能示例代码, 通过获取全局唯一的语音识别管理器recordRecoManager实现

//app.json
{
  ...
  "plugins": {
    ...
    "WechatSI": {
      "version": "0.0.7",
      "provider": "wx069ba97219f66d99"
        }
  }
}

//index.js
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
manager.onRecognize = function(res) {
    console.log("current result", res.result)
}
manager.onStop = function(res) {
    console.log("record file path", res.tempFilePath)
    console.log("result", res.result)
}
manager.onStart = function(res) {
    console.log("成功开始录音识别", res)
}
manager.onError = function(res) {
    console.error("error msg", res.msg)
}
manager.start({duration:30000, lang: "zh_CN"})

2.2 使用文本翻译功能示例代码,文本翻译目前支持的语言有 zh_CN(中国大陆) en_US(英语)

plugin.translate({
  lfrom:"en_US",
  lto:"zh_CN",
  content:"hello, this is the first time to test?",
  success: function(res) {
    if(res.retcode == 0) {
      console.log("result", res.result)
    } else {
      console.warn("翻译失败", res)
    }
  },
  fail: function(res) {
    console.log("网络失败",res)
  }
})

2.3 使用语音合成功能示例代码,语音合成支持的语言有 zh_CN(中国大陆),en_US(英文)

plugin.textToSpeech({
    lang: "zh_CN",
    tts: true,
    content: "一个常见的需求",
    success: function(res) {
        console.log("succ tts", res.filename)   
    },
    fail: function(res) {
        console.log("fail tts", res)
    }
})

3. 具体使用

3.1 在app.json文件使用插件

  "plugins": {
    "WechatSI": {
      "version": "0.3.5",
      "provider": "wx069ba97219f66d99"
    }
  },

3.2 页面的js文件

// 同声传译插件
var plugin = requirePlugin('WechatSI');
// 播报语音的API
var innerAudioContext = wx.createInnerAudioContext();
// ...

  // 语音播报
  speechText: function (e) {
    let that = this
    if (that.data.src == '') {
      return;
    }
    // 音频文件地址
    innerAudioContext.src = that.data.src
    // 播放速度
    innerAudioContext.playbackRate = 1
    // 开始播放
    innerAudioContext.onPlay((res) => {
		// ...
    })

    // 播放结束
    innerAudioContext.onEnded((res) => {
      // ...
    })

    // 播放出错
    innerAudioContext.onError((res) => {
      
    })
    // 播放停止
    innerAudioContext.onStop((res) => {
      
    })

    // 开始播报
    innerAudioContext.play();
  },

3.3 效果图

在这里插入图片描述> 3.4 微信小程序体验
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lilihewo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值