够意思博客够意思博客够意思博客

H5页面自动播放音乐,兼容安卓和苹果端

HTML部分

<div id="music">
    <audio id="bgmusic" src="/public/mp3/index.mp3" autoplay="autoplay"  style="display: block; width: 3%; height:3%;"></audio>
</div>

JS部分

<script src="/public/web/js/jquery-3.3.1.min.js"></script>
<script>
    $(function () {
       
        // $('#music').html('<audio autoplay="autoplay"><source src="/public/mp3/index.mp3" type="audio/mpeg"/></audio>');
        const _audio = document.getElementById('bgmusic');
        if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
            _audio.play();
        } else {
            if (document.addEventListener) {
                document.addEventListener("WeixinJSBridgeReady", function() {
                    _audio.play();
                }, false);
            } else if (document.attachEvent) {
                document.attachEvent("WeixinJSBridgeReady", function() {
                    _audio.play();
                });
                document.attachEvent("onWeixinJSBridgeReady", function() {
                    _audio.play();
                });
            }
        }
        document.addEventListener("touchstart", function(e) {
            _audio.play();
        }, false);

    })
</script>


本文为够意思原创文章,转载无需和我联系,但请注明来自够意思博客blog.go1s.cn:够意思博客 » H5页面自动播放音乐,兼容安卓和苹果端

加载中~