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

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();
  },
相关推荐
Greg_Zhong15 分钟前
微信小程序中便捷实现自定义底部tab栏
微信小程序·自定义底部tab
LXXgalaxy1 小时前
微信小程序“记住密码”功能的实现与落地 vue3+ts的细致解析
微信小程序·小程序·notepad++
Greg_Zhong1 小时前
微信小程序中使用【免费商用】字体的下载和初步认识和使用
微信小程序·阿里巴巴、站酷·腾讯云对象存储(cos)
克里斯蒂亚诺更新1 小时前
微信小程序 腾讯地图 点聚合 简单示例
微信小程序·小程序·notepad++
Geek_Vision2 小时前
鸿蒙原生APP接入小程序运行能力:数字园区场景实战复盘
微信小程序·harmonyos
CRMEB系统商城3 小时前
国内开源电商系统的格局与演变——一个务实的技术视角
java·大数据·开发语言·小程序·开源·php
2501_916007473 小时前
iOS逆向工程:详细解析ptrace反调试机制的破解方法与实战步骤
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张4 小时前
前端可视化大屏制作全指南:需求分析、技术选型与性能优化
前端·ios·性能优化·小程序·uni-app·iphone·需求分析
January12077 小时前
Taro3 + Vue3 小程序文件上传组件,支持 PDF/PPTX 跨端使用
小程序
OctShop大型商城源码7 小时前
商城小程序开源源码_大型免费开源小程序商城_OctShop
小程序·开源·商城小程序开源源码·免费开源小程序商城