Music Randomizer

15 Apr

Music on a website is fine so long as you do not have to listen to the same song over and over as some unfortunately do. Randomization makes short work of that. Moreover, this MIDI player will switch tracks if the same song has been playing too long.

<script language=”JavaScript”>
function scrollmidire()
midi.songname.value = midi.songname.value.substring(1,midi.songname.value.length) + midi.songname.value.charAt(0);
midiretimer = setTimeout(“scrollmidire()”, midirescrollspeed);
function stopmidire()
midire.src = “”;
midi.songname.value = ” – Stopped – “;
setcookiemidire(“midire”, “stop”);
function setmidire(file, name)
midire.src = midiredirectory + “/” + file;
midi.songname.value = ” You are listening to ” + name;
function nextmidire()
var randommusic = Math.round((Math.random() * 1000 + Math.random() * 500) % midirenum); // JavaScript is not the most random of languages; two randomizations helps greatly
case 0: setmidire(‘midi1.mid’, ‘First MIDI title’); break;
case 1: setmidire(‘midi2.mid’, ‘Second MIDI title’); break;
case 2: setmidire(‘midi3.mid’, ‘Third MIDI title’); break;
default: setmidire(‘midi.mid’, ‘Default MIDI title’); break; // this should never be reached, but just in case it is….
midiretimeout = setTimeout(“nextmidire()”, midirewait); // Milliseconds until track switches
setcookiemidire(“midire”, ” “);
function setcookiemidire(name,value)
var next = new Date();
next.setYear(next.getYear() + 1);
document.cookie = name + ‘=’ + value + ‘; expires=’ + next.toGMTString() + ‘;’;
function getcookiemidire(name)
var cookieFound = false;
var start = 0;
var end = 0;
var cookieString = document.cookie;
var i = 0;
while (i <= cookieString.length)
start = i;
end = start + name.length;
if (cookieString.substring(start,end) == name)
cookieFound = true;
start = end + 1;
end = document.cookie.indexOf(“;”,start);
if (end < start)
end = document.cookie.length;
return document.cookie.substring(start,end);
return “”;
var midirenum = 3; // Number of MIDIs; update every time a new one is added
var midirewait = 300000; // Milliseconds the same song will play for before another track begins
var midirescrollspeed = 100; // Low numbers make it scroll faster; large numbers, slower
var midiredirectory = “midis”; // Directory where your MIDIs are located
var midiretimer, midiretimeout; // timer declarations; leave them alone
document.write(‘<form name=”midi”>\n’);
document.write(‘<bgsound src=”” loop=”infinite” id=”midire”></bgsound>\n’);
document.write(‘<input type=”text” name=”songname” value=”You are listening to… ” readonly><br>\n’);
document.write(‘<input type=”button” name=”stop” title=”Stop” value=”•” onClick=”stopmidire()”>\n’);
document.write(‘<input type=”button” name=”next” title=”Next Track” value=”>” onClick=”nextmidire()”></form>\n’);
if(getcookiemidire(“midire”) == “stop”) stopmidire();
else nextmidire();

You should go through the script and change the script to meet your specialized needs. Just look for the conveniently placed CHANGE comments located in the script; there are only two of them, one at the bottom and one in the song list.
The entire thing is one large JavaScript, so it will easily fit into one imported script. If your song list ever becomes large, I recommend using it as an imported script for the sake of load speed.


2 Responses to “Music Randomizer”

  1. bjorn September 12, 2006 at 5:11 am #


    I have a mouseover button in my page that will be used to start/stop the music.
    The more I want it to be a random music file of says 10 songs.
    When clicked again it stops.
    When clicked again it plays a random files again, and so on…

    What script should i use ?

    Many thanks


  2. Putera Emas September 12, 2006 at 11:02 pm #

    Create your own button to run a random javascript code. Everytime you click on the button, it will randomize the song and play it. Hope this help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: