Generating a Dynamic XML or Text Playlist using PHP

June 5th, 2011 - Posted by Steve Marks to Flash, PHP, Web Development.

If you’ve ever tried to incorporate some kind of Flash music player into your website, you’ll probably notice that it will more than likely contain an option to use a playlist to play multiple songs in sequence. This can work really well if you’ve got a set number of songs that you want to play, not so great however if the songs are constantly changing, coming from a database or are user-contributed.

If this is the case then a standard XML or text file won’t do. Instead I show you below how to use PHP to generate this playlist on-the-fly.

The Solution

For the example below I’ll presume that the playlist is in XML format. Let’s look at the part where we embed the flash player and reference the playlist:

<param name="FlashVars" value="...&playlist=http://www.mydomain.com/playlist.php&..." />

Notice how we reference a PHP file? Let’s take a look at what this playlist.php file might look like. Depending on what you are trying to achieve this will no doubt change. In the example below I’m going to get the latest 5 songs from a MySQL table:

// specify that the output will be xml
header("Content-Type: text/xml");

// establish a connection to the MySQL database
$conn = mysql_connect("localhost", "user", "pass");
$db = mysql_select_db("song_database");

echo '<?xml version="1.0" encoding="UTF-8" ?>
<songs>';

// get 5 latest songs
$query = "SELECT `filename` FROM `songs` ORDER BY `dateAdded` DESC LIMIT 5";
$result = mysql_query($query);
while ($row=mysql_fetch_assoc($result)) {
    echo '<song>'.$row['filename'].'</song>';
}

echo '</songs>';

So there we have it. If a song was to get added, the playlist would update automatically without having to manually change the static playlist file.

Some Considerations

Caching

Flash has a tendancy to cache files that it uses which, as a result means the dynamic playlist being included might not update automatically. If this is a problem for you, one way to get around this would be to add a changing parameter to the end of the filename like so:

<param name="FlashVars" value="playlist=http://www.mydomain.com/playlist.php%3F<?php echo time(); ?>" />

PHP files not accepted as playlist FlashVar

Some music players specify that the playlist being used cannot be anything but a certain filetype. In the case of the XML playlist above we would get an error using playlist.php even though the final output is actually XML. To solve this scenario you can add a rule to your .htaccess file like so:

Redirect /playlist.xml http://www.mydomain.com/playlist.php

Then simply use playlist.xml in the FlashVar and (fingers crossed) the PHP playlist should be referenced instead.

Tags: , ,
This entry was posted on Sunday, June 5th, 2011 at 8:59 am by +Steve Marks and is filed under Flash, PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first