How to Play Random MP3 Files
by David J. Stewart
I searched the internet for years to find a way to play random MP3 files in a streaming M3U file, but couldn't find anything. I found bizarre codes and useless forum postings that all led to dead ends and code that didn't work. Today I was blessed by God and found the code I needed.
The following code works well and will allow you to create a playlist of MP3 or MIDI files, or a combination thereof, that will play randomly.
Simply create a new webpage (or use an existing webpage) to add the JavaScript code below to. I use Microsoft FrontPage to make my webpages.
Or make your own webpage...
Simply create a new text file in the main directory of your website, then rename the file's extension from “.txt” to “.htm” (which is a webpage file). Then copy the JavaScript code below and paste it into your webpage's HTML code between the body tags...
<body>
Paste here.
</body>
Here's the code below the line...
<script type="text/javascript">
var numberOfSongs = 6
var sound = new Array(numberOfSongs+1)
sound[0]= "http://www.soulwinning.info/sounds/gold_city_quartet-jesus_cares_for_you.mp3"
sound[1]= "http://www.soulwinning.info/sounds/communist_america.mp3"
sound[2]= "http://www.soulwinning.info/sermons/dr_hyles/forever_settled_in_heaven.mp3"
sound[3]= "http://www.soulwinning.info/sermons/dr_hyles/the_answer_is_in_the_manger.mp3"
sound[4]= "http://www.soulwinning.info/sounds/day_by_day-piano.mid"
sound[5]= "http://www.soulwinning.info/sounds/mike_zachary-all_your_anxiety.mp3"
function randomNumber(){
var randomLooper = -1
while (randomLooper < 0 || randomLooper > numberOfSongs || isNaN(randomLooper)){ randomLooper = parseInt(Math.random()*(numberOfSongs+1))
}
return randomLooper
}
var randomsub = randomNumber()
var soundFile = sound[randomsub]
document.write ('<EMBED src= "' + soundFile + '" hidden=true type="audio/mpeg" autostart=true loop=true>')
</script>
Please note in the preceding code that you need to replace the web address url's with your own MP3 or MIDI file locations.
Also, you can have as many files as you want, but you need to change the number (value) in the second line of the above code. There's a “6” right now in the value for “var numberOfSongs” because there are 6 audio url's addresses in the code (note that the zero also counts as a number).
If you add 2 more songs to the playlist, then you would need to change the value to 8 instead.
The next 2 songs would be added as...
sound[6]= "type new web address (URL) here"
sound[7]= "type new web address (URL) here"
And so forth...
Also, please notice that the playlist begins with a zero (0). So if you have 5 songs, then your value is 6. The zero counts as a number too.
Also, if you forget the parenthesis (“ ”) around even one value, the code WON'T work at all. Make sure that all your web url's are between parenthesis. If your code doesn't work, then you did something wrong. Here's a link to my gospel piano playlist, which you can follow as an example to get your random playlist going. God bless!
Start You Own Christian Website
Ye Must Be Born Again! | You Need HIS Righteousness!