Now that you've read about playlists, you know that we need to create a .m3u file that lists the URLs of the songs we want to play. But how can we put those songs in a random order for every user? The answer: we'll use PHP. Using PHP code, we can easily open up a .m3u file, shuffle the songs with PHP's built-in shuffle function, and deliver the result to the web browser as a playlist. We'll use a very cool feature of PHP called PATH_INFO to avoid creating any temporary playlist files. See the technical notes at the end of this entry for details on how it all works.
PHP is a great choice for this job. But users who are required to use Perl/CGI or ASP programming for web development can still use the ideas presented here in their own code. And the PATH_INFO trick demonstrated here works just as well for Perl CGI programs. But if you are using a free web hosting service that doesn't allow any kind of server-side programming... well, it's time to start paying for hosting that includes PHP support. Hosting services that include PHP can be really, really cheap these days. Less than $10 a month in fact. So skip a pizza once in a while and get a real web host. When you pay for your PHP hosting, the host doesn't stuff your page with annoying ads or, even worse, "spyware" programs that will make your visitors very angry.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. See How do I play a list of several MP3 songs on a web page? for complete instructions on how to make a playlist file.
2. Upload playlist.m3u to your web site. 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.
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.
srand(make_seed());
# Fetch our list of songs from a file.
$songs = file($playlist);
shuffle($songs);
# 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.
exit(0);
}
# 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);
}
?>MP3s Playing in Random Order
hidden="true"
autostart="true"
type="audio/mpeg"
loop="true">