2 回答

TA貢獻1853條經(jīng)驗 獲得超18個贊
創(chuàng)建一個按鈕并添加一個onclick屬性,移動selectRandom() 到一個按鈕onclick函數(shù),如下所示:
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>
添加回答
舉報