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

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();
  },
相关推荐
土土哥V_araolin11 小时前
双迪大健康新零售系统开发要点
小程序·个人开发·零售
2501_9159090612 小时前
不用越狱就看不到 iOS App 内部文件?使用 Keymob 查看和导出应用数据目录
android·ios·小程序·https·uni-app·iphone·webview
CHU72903514 小时前
扭蛋机盲盒小程序前端功能设计及核心玩法介绍
前端·小程序
职豚求职小程序15 小时前
中华财险笔试测评题库小程序刷题职豚2026新
小程序
阿珊和她的猫15 小时前
小程序双线程架构:逻辑层与视图层的协同运作机制
小程序·架构
源码ym7k资源15 小时前
在线家政系统(APP+小程序)源码:为您提供专业的本地服务
小程序
2601_9520137615 小时前
家政服务小程序预约上门服务维修保洁上门服务在线派单技师入驻-ym7K
小程序
2501_9159214315 小时前
常用iOS性能测试工具大全及使用指南
android·测试工具·ios·小程序·uni-app·cocoa·iphone
一字白首16 小时前
小程序组件化进阶:从复用到通信的完整指南DAY04
前端·小程序·apache
是Winky啊16 小时前
【逆向+爬虫】获取小程序信息
小程序