小程序中使用微信同声传译插件实现语音识别、语音合成、文本翻译功能----文本翻译(三)

官方文档链接:https://mp.weixin.qq.com/wxopen/plugindevdoc?appid=wx069ba97219f66d99&token=370941954&lang=zh_CN#-

要使用插件需要先在小程序管理后台的设置->第三方设置->插件管理中添加插件,目前该插件仅认证后的小程序。

文本翻译功能

文本翻译目前支持的语言有 zh_CN(中国大陆) en_US(英语)。

translate(obj)

参数说明:

1、lfrom:文本语言 zh_CN(中国大陆) en_US(英语),String类型,必填项;

2、lto:目标语言 zh_CN(中国大陆) en_US(英语),String类型,必填项;

3、content:需要被翻译的文本内容,后台限制1000字节大小,String类型,必填项;

4、tts:是否对翻译结果进行语音合成,Boolean类型,默认为false,不进行语音合成;

5、success:调用成功时触发的回调,Function类型;

回调结果说明

retcode::retcode == 0 时翻译成功,Int类型;

origin:原始文本,String类型;

result:翻译结果,String类型

filename:语音合成返回的语音地址,仅支持合成中文语音,String类型;

expired_time:语音合成链接超时时间戳 如1525930552超时后无法播放,可使用时间为3小时,Int类型;

翻译成功,合成失败时success回调,success返回码说明:

0 翻译合成成功

-10006 翻译成功,合成内部错误

-10007 翻译成功,传入了不支持的语音合成语言

-10008 翻译成功,语音合成达到频率限制

6、fail:调用失败时触发的回调,Function类型;

回调结果说明

retcode:错误码,Int类型;

msg:错误信息,String类型。

错误码说明

-10001 语言检查错误

-10002 输入的待翻译内容格式不正确

-10003 传入过长的待翻译文本内容

-10004 翻译内部逻辑错误

-10005 请求发送失败,请检查网络

-40001 接口调用频率达到限制,请联系插件开发者

7、complete:接口调用结束的回调函数(调用成功、失败都会执行),Function类型。

使用:

1、注册插件

在app.json中注册插件

“plugins”: {

“WechatSI”: {

“version”: “0.3.5”,

“provider”: “wx069ba97219f66d99”

}

},

2、在页面中引入插件

//引入插件:微信同声传译

const plugin = requirePlugin(‘WechatSI’)

3、在上述1、2步骤完成后实现文本翻译

plugin.translate({
  lfrom:"en_US", //原语言      
  lto:"zh_CN", // 翻译的目标语言
  content:"hello, this is the first time to test?", //需要翻译的内容
  tts: true, //对翻译结果进行语音合成
  success: (res) => {
    console.log(res);
    if(res.retcode == 0) {
      console.log("原始文本=>", res.origin)
      console.log("翻译结果=>", res.result)
      console.log("语音合成返回的语音地址=>", res.filename)
      console.log("语音合成超时后无法播放时间戳=>", res.expired_time)
      // 1、设置tts为true,播放合成的语音
      const backgroundAudioManager = wx.getBackgroundAudioManager()
      backgroundAudioManager.src = res.filename  
      backgroundAudioManager.title = '文本播放'  //必须 否则语音无法播放
    } else {
      console.warn("翻译失败", res)
    }
  },
  fail: function(res) {
    console.log("网络失败",res)
  }
})

案例实现代码:

//引入插件:微信同声传译
const plugin = requirePlugin('WechatSI')
Page({ /**
   * 生命周期函数--监听页面显示
   */
  onShow() { this.textTranslate("积极的语言塑造积极的思维,积极的思维塑造积极的人生", "zh_CN", "en_US")
    // this.textTranslate()
  },
  textTranslate(content = "positive  language, positive  mind, positive  life", lfrom = "en_US", lto = "zh_CN"){ plugin.translate({ lfrom,
      lto,
      content,
      tts: true, //对翻译结果进行语音合成
      success: (res) => { console.log(res);
        if(res.retcode == 0) { console.log("原始文本=>", res.origin)
          console.log("翻译结果=>", res.result)
          console.log("语音合成返回的语音地址=>", res.filename)
          console.log("语音合成超时后无法播放时间戳=>", res.expired_time)
          // 1、设置tts为true,播放合成的语音
          const backgroundAudioManager = wx.getBackgroundAudioManager()
          backgroundAudioManager.src = res.filename  
          backgroundAudioManager.title = '文本播放' 
          // 2、使用语音合成方法播报
          // this.playTextToVoice(res.result)
        } else { console.warn("翻译失败", res)
        }
      },
      fail: function(res) { console.log("网络失败",res)
      }
    })
  },
  // 文字转语音
  playTextToVoice(content){ //创建内部 audio 上下文 InnerAudioContext 对象。
    this.innerAudioContext = wx.createInnerAudioContext();
    const that = this;
    plugin.textToSpeech({ // 调用插件的方法
      lang: 'zh_CN',
      // lang: 'en_US',
      content,
      success: function (res) { that.playAudio(res.filename);
      }
    });
  },
  // 播报语音
  playAudio(e) { this.innerAudioContext.src = e; //设置音频地址
    this.innerAudioContext.play(); //播放音频
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { this.innerAudioContext && this.innerAudioContext.stop();
    this.innerAudioContext && this.innerAudioContext.destroy();
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { this.innerAudioContext && this.innerAudioContext.stop();
    this.innerAudioContext && this.innerAudioContext.destroy();
  },
})

具体案例代码亦可参考:https://gitee.com/mei-ruohan/mini-program-collection/tree/master/pages/texttranslate