2 回答
TA贡献1853条经验 获得超18个赞
创建一个按钮并添加一个onclick属性,移动selectRandom() 到一个按钮onclick函数,如下所示:
var player = document.getElementById("audioplayer");
var lastSong = null;
var selection = null;
var playlist = ["https://www.soundjay.com/button/sounds/beep-07.mp3", "https://www.soundjay.com/button/sounds/button-2.mp3", "https://www.soundjay.com/button/sounds/button-3.mp3"]; // List of Songs
function start() {
player.play();
player.addEventListener("ended", selectRandom);
function selectRandom(){
while(selection == lastSong){ // Repeat until different song is selected
selection = Math.floor(Math.random() * playlist.length);
}
lastSong = selection; // Remember last song
player.src = playlist[selection]; // Tell HTML the location of the new Song
}
player.autoplay=true;
selectRandom();
}
<audio id="audioplayer" controls ></audio>
<button onclick="start()"> Play </button>
添加回答
举报