勤挖利-在html中插入mp3音频的代码-MP3在线听音乐播放器代码
<div id="audioBox">
<script type="text/javascript">
window.onload = function(){
var arr = ["https://sharefs.yun.kugou.com/201911122244/321f7099a0190b28890c6884d76c4512/G121/M07/1C/04/uQ0DAFpEMFCAMqXcAC8r8qhrqBM209.mp3","https://sharefs.yun.kugou.com/201911122247/5f103759d0d7907e84dabae14893a369/G137/M05/0B/03/aZQEAFuJUeOAMV3cAE7o_QMU--I340.mp3","https://sharefs.yun.kugou.com/201911122241/1344a9feede470f4cb4c69344bda6707/G137/M07/04/09/aZQEAFtmuI6AEzLQAFC-nzV44q4281.mp3","https://sharefs.yun.kugou.com/201911122246/5e32095bb367f581b3187d1b2606967d/G064/M09/13/07/4IYBAFdia46Ac3lVAE5Bnah9TmU624.mp3"]; //把需要播放的歌曲从后往前排,这里已添加两首音乐,可继续添加多个音乐
var myAudio = new Audio();
myAudio.preload = true;
myAudio.controls = true;
myAudio.src = arr.pop(); //每次读数组最后一个元素
myAudio.addEventListener('ended', playEndedHandler, false);
myAudio.play();
document.getElementById("audioBox").appendChild(myAudio);
myAudio.loop = false;//禁止循环,否则无法触发ended事件
function playEndedHandler(){
myAudio.src = arr.pop();
myAudio.play();
console.log(arr.length);
!arr.length && myAudio.removeEventListener('ended',playEndedHandler,false);//只有一个元素时解除绑定
}
}
</script>
</div>