HTML5中的audio在手机端和微信端的不能自动播放

再做H5页面的时候,发现audio在手机端和微信端添加了autoplay以后还是不可以自动播放,这是因为手机端为了节约流量所设置的

通常解决方法是给一个交互事件:

标签:
<audio loop src="/photo/aa.mp3" id="audio" autoplay preload="auto">
该浏览器不支持audio属性</audio>

解决方法:
//--创建页面监听,等待微信端页面加载完毕 触发音频播放

document.addEventListener('DOMContentLoaded', function () {
    function audioAutoPlay() {
        var audio = document.getElementById('audio');
            audio.play();
        document.addEventListener("WeixinJSBridgeReady", function () {
            audio.play();
        }, false);
    }
    audioAutoPlay();
});
//--创建触摸监听,当浏览器打开页面时,触摸屏幕触发事件,进行音频播放
document.addEventListener('touchstart', function () {
    function audioAutoPlay() {
        var audio = document.getElementById('audio');
            audio.play();
    }
    audioAutoPlay();
});
还有一种针对苹果的手机微信端的解决方法
第一步:引入js文件
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

第二步:配置文件

<script>
        function autoPlayVideo(){
            wx.config({
                debug:false,
                appId:"",
                timestamp:1,
                nonceStr:"",
                signature:"",
                jsApiList:[]
            });
            wx.ready(function(){
                var autoplayVideo=document.getElementById("audio");
                autoplayVideo.play()
            })
        };
        autoPlayVideo();
</script>
这样在网络稳定的情况下是可以自动播放的。
发布了55 篇原创文章 · 获赞 43 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_21137441/article/details/102879630