小程序中的语音播放

最新遇到个新的需求,在小程序中特定情况下做语音播放,此种情况用到了微信同声传译插件,具体操作流程如下:

1.信公众平台=>设置=>第三方设置=>插件管理=>添加插件=>'微信同声传译'=>添加(目前暂不支持个人开发者使用):

2.点击详情,查看相关的版本号以及appid

3.在manifest.json=>源码视图=>添加如下代码

//微信同声传译插件引入
         "plugins" : {
            "WechatSI" : {
                "version" : "0.3.5",
                "provider" : "wx069ba97219f66d99"
            }
        },

4.相关代码实现:

<script>
    const plugin = requirePlugin('WechatSI');
    const innerAudioContext = wx.createInnerAudioContext();
    
    export default {
        data() {
            return {
                timer:null
            }
        },
        onLaunch: function(options) {
            
        },
        onShow: function() {
            var that = this
            that.getIsNewOrder()
            this.timer = setInterval(that.getIsNewOrder,60000)
            
        },
        onHide: function() {
            console.log('App Hide');
            clearInterval(this.timer)
        },
        methods:{
            //这是相关的小程序的语音播报的方法  2024-2-28 mxj新增
            //请求的接口的方法
            getIsNewOrder() {
                var that = this
                this.$http.get('api/shop/shop_order/latest_order').then(function(data){
                    console.log(data,'====')
                    if(data.code == 0){
                        if(data.status == 1){
                            that.speakCan()
                        }
                    }
                }).catch(function(res){});
            },
            //语音播报的方法
            speakCan(){
                let _this = this;
                console.log('111222333');
                plugin.textToSpeech({
                    lang: 'zh_CN',
                    tts: true,
                    content: '您有新的订单',
                    success: function (res) {
                        console.log('succ tts', res.filename);
                            _this.yuyinPlay(res.filename)
                    },
                    fail: function (res) {
                        console.log('fail tts', res);
                    },
                    complete: function (res) {
                        console.log('complete tts', res);
                    }
              });
            },
            yuyinPlay(src) {
                if (src == '') {
                    return;
                }
                innerAudioContext.autoplay = true;
                innerAudioContext.src = src; //设置音频地址
                innerAudioContext.play(); //播放音频
            },
        },
    }
</script>

参考地址:

https://www.cnblogs.com/zaijin-yang/p/17384506.html

 

posted @ 2024-02-28 16:31  星宝攸宁  阅读(102)  评论(0编辑  收藏  举报