微信小程序调用微信同声传译插件(中文识别)

微信小程序后台配置添加微信同声传译插件不再详述,具体可自行百度。
app.json添加这段代码:

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

util.js中添加

var plugin = requirePlugin("WechatSI")

var innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.onError((res) => {
  // 播放音频失败的回调
})

function playTTS(text) {
  //need to add WXAPP plug-in unit: WechatSI
  plugin.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,
}

新建页面,在新页面的js里面添加这段代码

// Page外
var pageSelf = undefined;
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    manager.onRecognize = function (res) {
      // console.log('manager.onRecognize')
      // console.log(res)
      // wx.showToast({
      //   title: res.result,
      // })
      // cons.log("current result", res.result)
    }
    manager.onStop = function (res) {
      console.log('manager.onStop')
      console.log(res)//语音识别信息打印
      wx.showToast({
        title: res.result,
      })
      // 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) {
      console.log('manager.onError')
      console.log(res)//报错信息打印
      wx.showToast({
        title: res.msg,
      })
      // UTIL.log("error msg", res.msg)
    }
  },
  //添加两个方法
    touchdown_plugin: function() {
    var _this = this
    // UTIL.stopTTS();
    manager.start({
      duration: 30000,
      lang: "zh_CN"
    })
  },
  //手指松开
  touchup_plugin: function() {
    manager.stop();
    wx.showToast({
      title: '正在识别……',
      icon: 'loading',
      duration: 2000
    })
  },

在新页面的wxml里面添加这段代码

<button type="primary" style="border-radius: 20px;" class="btn-style" bindtouchstart="touchdown_plugin" bindtouchend="touchup_plugin">按下录音,松开结束</button>
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值