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

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();
  },
相关推荐
二J1 小时前
管理100个小程序-很难吗
android·小程序
qq_357389632 小时前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码,医护小程序
小程序
橘猫云计算机设计14 小时前
springboot-基于Web企业短信息发送系统(源码+lw+部署文档+讲解),源码可白嫖!
java·前端·数据库·spring boot·后端·小程序·毕业设计
曲江涛19 小时前
爆改 toxml 组件 支持数据双向绑定 解决数据刷新问题
notepad++
跨时代科技1 天前
洗车小程序系统前端uniapp 后台thinkphp
小程序
依辰1 天前
小程序SAAS产品定制化需求解决方案
前端·javascript·微信小程序
fakaifa1 天前
【最新版】沃德代驾源码全开源+前端uniapp
前端·小程序·uni-app·开源·php·沃德代驾·代驾小程序
18538162800余。1 天前
NFC 碰一碰实现视频源码,网页与小程序协同
小程序
换日线°2 天前
微信小程序拖拽排序有效果图
微信小程序
码起来呗2 天前
基于微信小程序的走失儿童帮助系统-项目分享
微信小程序·小程序