Audio shuffle

From XPUB & Lens-Based wiki

Using SoundManager2

<!DOCTYPE html>
<html>
<title></title>
<script src="soundmanager/script/soundmanager2-jsmin.js"></script>
<script>

var shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

var playlist = [
    "audio1.mp3",
    "audio2.mp3",
    "audio3.mp3"
];
shuffle(playlist);

var x = 0;

function play () {
    soundManager.play("mySound", {
        url: playlist[x]
    });
}

soundManager.setup({
  url: 'soundmanager/swf',
  flashVersion: 9, // optional: shiny features (default = 8)
  useFlashBlock: false, // optionally, enable when you're ready to dive in
  onready: function() {
    soundManager.createSound({
         id: 'mySound', // required
         url: playlist[x], // required
         // optional sound parameters here, see Sound Properties for full list
         volume: 50,
         autoPlay: false,
         onfinish: function () {
            // THIS PART HAPPENS WHEN A FILE ENDS...
            x = x+1; // ADD ONE TO THE X
            if (x >= playlist.length) {
                 // RESET THE X WHEN IT GETS TOO BIG
                x = 0;
                shuffle(playlist);
                // SHUFFLE THE PLAYLIST
            }
            play(); // PLAY THE CURRENT FILE (loop)
         }
    });
    play();
  }
});

</script>
<body>
<h1>1-2-3</h1>

</body>

</html>