Computer >> Máy Tính >  >> Lập trình >> Javascript

Cách phát ngẫu nhiên âm thanh HTML5

Để phát ngẫu nhiên, hãy thêm các bài hát như sau:

init ([
   'https://demo.com/songs/song1.mp3,
   'https://demo.com/songs/song2.mp3,
   'https://demo.com/songs/song3.mp3
]);

Sử dụng phần sau để chơi ngẫu nhiên bằng Math.random:

function displayRandom() {
   var audio = Math.floor(Math.random() * (collection.length));
   audio = collection[audio];
   audio.play();
   setTimeout(loop,audio.duration*1000);
}