How to play HTML5 Audio Randomly

JavascriptWeb DevelopmentFront End Scripts

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);
}
raja
Published on 11-Apr-2018 15:20:54
Advertisements