文章詳情頁
H5頁面使用audio標(biāo)簽播放音頻
瀏覽:288日期:2022-06-12 13:55:09
H5頁面播放音樂其實(shí)很簡(jiǎn)單,只需要用<audio>這個(gè)標(biāo)簽就行十分方便。
路徑選在音樂所在位置就行了。
<audio id="bgMusic" src="js/2.mp3" autoplay preload loop="loop"></audio>
關(guān)于點(diǎn)擊按鈕音樂開啟/停止播放的效果做了個(gè)簡(jiǎn)單的例子
<a id="audioBtn" οnclick="autoPlay()"></a> ?<audio id="bgMusic" src="js/2.mp3" autoplay preload loop="loop"></audio> ?css.pause { ? ? ? ? ? ? ?height: 50px; ? ? ? ? ? ? ?background: url(images/musicbtn.png) no-repeat; ? ? ? ? ? ? ?display: block; ? ? ? ? ? ? ?background-position: 0 bottom; ? ? ? ? ?} ?? ? ? ? ? ?.play { ? ? ? ? ? ? ?height: 50px; ? ? ? ? ? ? ?background: url(images/musicbtn.png) no-repeat; ? ? ? ? ? ? ?display: block; ? ? ? ? ?} js function autoPlay() { ? ? ? ? ? ? ?var myAuto = document.getElementById("bgMusic"); ? ? ? ? ? ? ?var btn = document.getElementById("audioBtn"); ? ? ? ? ? ? ?if (myAuto.paused) { ? ? ? ? ? ? ? ? ?myAuto.play(); ? ? ? ? ? ? ? ? ?btn.classList.remove("pause"); ? ? ? ? ? ? ? ? ?btn.classList.add("play"); ? ? ? ? ? ? ?} else { ? ? ? ? ? ? ? ? ?myAuto.pause(); ?? ? ? ? ? ? ? ? ? ?btn.classList.remove("play"); ? ? ? ? ? ? ? ? ?btn.classList.add("pause"); ? ? ? ? ? ? ?} ?? ? ? ? ? ?}
不過只有這個(gè)如果是移動(dòng)端用到,iphone不會(huì)開啟是自動(dòng)播放需
<script type="text/javascript"> function audioAutoPlay() { var audio = document.getElementById("bgMusic"), play = function () { audio.play(); document.removeEventListener("touchstart", play, false); }; audio.play(); document.addEventListener("WeixinJSBridgeReady", function () { play(); }, false); document.addEventListener("YixinJSBridgeReady", function () { play(); }, false); document.addEventListener("touchstart", play, false); } </script>
到此這篇關(guān)于H5頁面使用audio標(biāo)簽播放音頻的文章就介紹到這了,更多相關(guān)audio標(biāo)簽播放音頻內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
排行榜
