View Full Version : how to randomize mp3 playlist, instead of ordered playlist

12-09-2010, 11:18 PM
I have a set of mp3 music files that i wish to have played on my site, but I would like them to play in a random order.

How could I do this ?

12-30-2010, 11:42 PM
What are you using to play them right now?

01-10-2011, 04:53 PM
Why do you want them to play in random order?

01-11-2011, 09:10 AM
Follow these steps to play MP3 files in a random order on your web page:

1. Create a playlist file called playlist.m3u containing all of the songs you want, in any order.

2. Upload playlist.m3u to your website. When you do, make a note of the file system path to playlist.m3u on the server. If your FTP program shows that the folder is /home/sites/yourname/www.example.com/examples/, then the path to playlist.m3u is /home/sites/yourname/www.example.com/examples/playlist.m3u. Note: this is NOT a URL. It is the place on the server's hard drive where the file lives. If you don't understand this, reread it until you do!

3. Create the file randomsongs.php, containing the following. There must be ABSOLUTELY NO BLANK LINES OR WHITE SPACES AT THE BEGINNING! Otherwise it will be too late for the script to output a playlist instead of an HTML page when that is appropriate.

$playlist = "/home/sites/myname/www.example.com/examples/playlist.m3u";
if ($_SERVER['PATH_INFO'] == "/playlist.m3u") {
# This a request for the actual playlist.
} else {
# Fall through to end of script and display
# the player HTML.
function playlist() {
header("Content-type: audio/mpeg");

# Needed for PHP versions OLDER than 4.2.0 only.
# If your host still has PHP older than 4.2.0, shame on them.
# Find a better web host.

# Fetch our list of songs from a file.
$songs = file($playlist);
# Now output the URLs in random order.
foreach ($songs as $song) {
# Remove newline and any other leading and trailing
# whitespace from URL of song.
$song = trim($song);
echo "$song\n";
# Now exit before any HTML is produced.
# Needed only for very old versions of PHP,
# see srand call earlier.
function make_seed()
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);

<title>MP3s Playing in Random Order</title>
<h1 align="center">MP3s Playing in Random Order</h1>
<embed src="/examples/randomsongs.php/playlist.m3u"

4. At the top of randomsongs.php change the setting of the $playlist variable to match the file system path of your playlist file... which you wrote down in step two.

5. randomsongs.php assumes you will be placing your PHP page in a folder on your website called /examples. If that's not right, change the src attribute of the embed element. DO NOT remove /playlist.m3u from the end of the URL. If you want to know why it's there, read the technical notes at the end of this entry.

6. Upload randomsongs.php to your web server.

7. Upload your .mp3 files if you have not already done so.

8. Access randomsongs.php with your web browser. In our example, the URL would be http://www.example.com/examples/randomsongs.php, but of course your URL will be different. Your songs will begin to play in a random order.