微信小程序订单语音播报

需要先在微信公众平台下载插件微信同声传译

《微信小程序订单语音播报》

index.js

var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
const app = getApp()
Page({ 
  data: { 
    content: '您有新的订单',
  },
  onReady: function () { 
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError(function (res) { 
      wx.showToast({ 
        title: '语音播放初始化失败',
      })
    })
  },
  /** * 生命周期函数--监听页面加载 */
  onLoad: function (options) { 
    this.readText()
  },
  //阅读文字
  readText: function () { 
    var content = this.data.content;
    var that = this;
    plugin.textToSpeech({ 
      lang: "zh_CN",
      tts: true,
      content: content,
      success: function (res) { 
        that.setData({ 
          auidoSrc: res.filename
        })
        that.readStart();
      },
      fail: function (res) { 
        wx.showToast({ 
          title: '语音转换失败',
        })
      }
    })
  },
  //开始阅读文字
  readStart: function () { 
    this.innerAudioContext.src = this.data.auidoSrc //设置音频地址
    this.innerAudioContext.play(); //播放音频
  },
})
 

index.html

<button type="warn" bindtap="readText" size="mini">语音阅读</button>

app.json

"plugins": { 
    "WechatSI": { 
      "version": "0.3.4",
      "provider": "wx069ba97219f66d99"
    }
  },
    原文作者:Faaaaaaq
    原文地址: https://blog.csdn.net/fanganqing/article/details/117406237
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞