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

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

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

语音合成功能

语音合成支持的语言有 zh_CN(中国大陆),en_US(英文)。

textToSpeech(obj)

参数说明:

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

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

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

回调结果说明:

retcode:retcode == 0 时语音合成成功,Int类型;

origin: 原始文本,String类型;

filename:语音合成返回的语音地址,可自行下载使用,String类型;

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

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

回调结果说明

retcode:错误码,Int类型;

msg:错误信息,String类型。

错误码说明

-20001 语音合成语言格式出错

-20002 输入的待合成格式不正确

-20003 语音合成内部错误

-20005 网络错误

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

使用

1、注册插件

在app.json中注册插件

"plugins": {

"WechatSI": {

"version": "0.3.5",

"provider": "wx069ba97219f66d99"

}

},

2、在页面中引入插件

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

const plugin = requirePlugin('WechatSI')

3、在上述1、2步骤完成后实现语音合成

// 文字转语音

playTextToVoice(){

//创建内部 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(); //播放音频

},

4、如果在页面隐藏或卸载时不再播放合成的语音可将其关闭或销毁播放实例

onHide() {

this.innerAudioContext.stop();

this.innerAudioContext.destroy();

},

案例实现代码:

javascript 复制代码
//引入插件:微信同声传译
const plugin = requirePlugin('WechatSI')

Page({
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 播报
    this.playTextToVoice()
  },

  // 文字转语音
  playTextToVoice(){
    //创建内部 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/texttvoice

相关推荐
下雪天的夏风3 分钟前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
diygwcom15 分钟前
electron-updater实现electron全量版本更新
前端·javascript·electron
Hello-Mr.Wang31 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
艾小逗1 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
程序员阿龙1 小时前
【2025】儿童疫苗接种预约小程序(源码+文档+解答)
小程序·毕业设计·小程序开发·预约小程序·疫苗管理小程序·出勤数据分析·智能考勤
程序员凡尘1 小时前
完美解决 Array 方法 (map/filter/reduce) 不按预期工作 的正确解决方法,亲测有效!!!
前端·javascript·vue.js
818源码资源站1 小时前
表情包创作、取图小程序端(带流量主)
小程序
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
(⊙o⊙)~哦6 小时前
JavaScript substring() 方法
前端
无心使然云中漫步7 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript