微信小程序-文字转语音(播放及暂停)

1、使用微信小程序的同声传译功能

小程序平台-设置-第三方设置-插件管理-新增同声传译插件
小程序app.json文件配置
javascript 复制代码
  "plugins": {
    "WechatSI": {
      "version": "0.3.5",
      "provider": "wx069ba97219f66d99"
    }
  },

小程序中使用

.xml
javascript 复制代码
 <view class="action-box">
          <view class="action-btn" wx:if="{{!isPlaying}}" bind:tap="textToSpeech">播放</view>
          <view class="action-btn" wx:else bind:tap="pauseSpeech">暂停</view>
 </view>
.js
javascript 复制代码
var plugin = requirePlugin("WechatSI")
 data: {
    audioContext: null,
    isPlaying: false,
    text:'这是一个微信小程序文字转语音的功能'
    }
      // 语音播报
  textToSpeech(e) {
    let name = e.currentTarget.dataset.name
    let that = this
    if (this.audioContext) {
      this.audioContext.destroy()
    }
    plugin.textToSpeech({
      lang: "zh_CN",
      tts: true,
      content: name,
      success: function (res) {
        that.audioContext = wx.createInnerAudioContext();
        that.audioContext.src = res.filename;
        that.audioContext.play();
        that.setData({
          isPlaying: true
        })
        that.audioContext.onEnded(() => {
          //播放结束
          that.setData({
            isPlaying: false
          })
        })
      },
      fail: function (res) {
        console.log("fail", res)
      }
    })
  },
  // 暂停语音播报
  pauseSpeech() {
    console.log(111,this.data.isPlaying,this.audioContext);
    if (!this.data.isPlaying) return;
    this.setData({
      isPlaying: false
    })
    this.audioContext && this.audioContext.pause();
  },
相关推荐
JELEE.11 小时前
原生微信小程序开发笔记
笔记·微信小程序
蓝黑20201 天前
从经纬度获取地理信息以及从地名获取经纬度
小程序
码云数智-园园1 天前
和小鹅通一样的平台有哪些
微信小程序
吴声子夜歌1 天前
小程序——逻辑层
小程序
花木偶1 天前
小迪网安:APP攻防-Day1
安全·小程序
吴声子夜歌3 天前
小程序——生命周期函数和事件处理函数
服务器·前端·小程序
hnxaoli3 天前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股
码云数智-大飞3 天前
自助建站系统哪个好?快速建站哪个平台好
微信小程序
吴声子夜歌3 天前
小程序——项目结构
小程序
大黄说说3 天前
微信小程序制作平台有哪些?SaaS小程序搭建平台推荐
微信小程序