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

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();
  },
相关推荐
万岳软件开发小城3 小时前
陪诊APP+小程序一体化搭建方案:如何低成本打造医疗陪护平台?
小程序·医院陪诊系统源码·陪诊软件开发·陪诊平台开发·陪诊小程序开发
lichenyang4537 小时前
Expo 小程序媒体库功能设计与实现记录
小程序
经济元宇宙10 小时前
2026混合开发工具选型:小程序生态适配测评
小程序
fengyehongWorld1 天前
Notepad++ NppExec插件的使用
notepad++
lpfasd1231 天前
微信小程序虚拟支付(道具直购)踩坑全记录:从-15005到支付成功
微信小程序·小程序
crazy_wsp1 天前
使用AI从0到1上线微信小程序
人工智能·微信小程序·小程序
小宋的踩坑日记1 天前
全网最全!Tailwind/Unocss 类名速查表,前端开发必备神器!
css·小程序·前端框架
低代码布道师1 天前
健身房私教课小程序需求规格说明书
小程序·规格说明书
m0_462803882 天前
培训分组与记分操作指南
微信小程序
浩冉学编程2 天前
微信小程序中基于java后端实现官方的文本内容安全识别msgSecCheck
java·前端·安全·微信小程序·小程序·微信公众平台·内容安全审核