HTML5 Audio to Play Randomly



To play randomly, add the songs like this −

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

Use the following to play randomly using Math.random −

function displayRandom() {
   var audio = Math.floor(Math.random() * (collection.length));
   audio = collection[audio];
   audio.play();
   setTimeout(loop,audio.duration*1000);
}
Updated on: 2020-06-25T07:38:46+05:30

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements