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

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();
  },
相关推荐
HERR_QQ7 小时前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
racerun15 小时前
小程序导航设置更多内容的实现方法
小程序
说私域15 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg66815 小时前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳16 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww12118 小时前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero20 小时前
微信小程序41~50
微信小程序·小程序
走,带你去玩1 天前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉1 天前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序