这篇文章主要介绍了解决HTML5中的audio在手机端和微信端的不能自动播放问题,需要的朋友可以参考下

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

解决HTML5中的audio在手机端和微信端的不能自动播放问题(html audio不能自动播放)  微信audio html audio 第1张

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

标签:

<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>

这样在网络稳定的情况下是可以自动播放的。

总结

以上所述是小编给大家介绍的解决HTML5中的audio在手机端和微信端的不能自动播放问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

转载请说明出处
知优网 » 解决HTML5中的audio在手机端和微信端的不能自动播放问题(html audio不能自动播放)

发表评论

您需要后才能发表评论