H5頁面使用audio標簽播放音頻
更新時間:2023年03月07日 08:22:47 作者:黃啊碼
這篇文章主要介紹了H5頁面使用audio標簽播放音頻的相關(guān)資料,需要的朋友可以參考下
H5頁面播放音樂其實很簡單,只需要用<audio>這個標簽就行十分方便。
路徑選在音樂所在位置就行了。
<audio id="bgMusic" src="js/2.mp3" autoplay preload loop="loop"></audio>
關(guān)于點擊按鈕音樂開啟/停止播放的效果做了個簡單的例子
<a class="play" id="audioBtn" style="cursor:pointer;" ο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"); ? ? ? ? ? ? ?} ?? ? ? ? ? ?}
不過只有這個如果是移動端用到,iphone不會開啟是自動播放需
<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標簽播放音頻的文章就介紹到這了,更多相關(guān)audio標簽播放音頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

CSS網(wǎng)頁布局入門教程14:縱向下拉及多級彈出式菜單
CSS網(wǎng)頁布局入門教程14:縱向下拉及多級彈出式菜單...
2007-09-09 
CSS網(wǎng)頁布局入門教程6:左列固定,右列寬度自適應(yīng)
CSS網(wǎng)頁布局入門教程6:左列固定,右列寬度自適應(yīng)...
2007-09-09