App下載

audio在手機(jī)端和微信端的不能自動(dòng)播放怎么解決?問(wèn)題解決方法總結(jié)!

有啤酒肚的仙女 2021-08-09 16:39:38 瀏覽數(shù) (5120)
反饋

不知道大家在再做H5頁(yè)面的時(shí)候,會(huì)不會(huì)發(fā)現(xiàn)audio在手機(jī)端和微信端添加了autoplay以后還是不可以自動(dòng)播放,那么今天我們就來(lái)討論下有關(guān)于:“audio在手機(jī)端和微信端的不能自動(dòng)播放怎么解決?”這個(gè)問(wèn)題的解決方法是怎么樣的吧! 

通常解決方法是給一個(gè)交互事件:

標(biāo)簽:

<audio loop src="/photo/aa.mp3" id="audio" autoplay preload="auto">

該瀏覽器不支持audio屬性</audio>

解決方法:

//--創(chuàng)建頁(yè)面監(jiān)聽(tīng),等待微信端頁(yè)面加載完畢 觸發(fā)音頻播放
document.addEventListener('DOMContentLoaded', function () {
    function audioAutoPlay() {
        var audio = document.getElementById('audio');
            audio.play();
        document.addEventListener("WeixinJSBridgeReady", function () {
            audio.play();
        }, false);
    }
    audioAutoPlay();
});
//--創(chuàng)建觸摸監(jiān)聽(tīng),當(dāng)瀏覽器打開(kāi)頁(yè)面時(shí),觸摸屏幕觸發(fā)事件,進(jìn)行音頻播放
document.addEventListener('touchstart', function () {
    function audioAutoPlay() {
        var audio = document.getElementById('audio');
            audio.play();
    }
    audioAutoPlay();
});

還有一種針對(duì)蘋(píng)果的手機(jī)微信端的解決方法

第一步:引入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>

這樣在網(wǎng)絡(luò)穩(wěn)定的情況下是可以自動(dòng)播放的。

總結(jié)

那么以上就是今天小編對(duì)于“audio在手機(jī)端和微信端的不能自動(dòng)播放怎么解決?”這個(gè)問(wèn)題解決方法的分享!希望對(duì)大家有所幫助,有喜歡html5的小伙伴們都可以在W3Cschool進(jìn)行搜索自己喜歡的語(yǔ)言學(xué)習(xí)! 

0 人點(diǎn)贊