Flash

Generating a Dynamic XML or Text Playlist using PHP

Posted in Flash, PHP, Web Development on June 5th, 2011 by Steve Marks – Be the first to comment

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.

Share

When JW Player Doesn’t Play On IE

Posted in Flash, Javascript / jQuery, Web Development on June 22nd, 2010 by Steve Marks – 2 Comments

When JW Player Doesn't Play in IE

I’ve used the JW Player flash player for lots of projects now. It’s free, customisable enough and has a lot of documentation, plus community support, to help when something isn’t working right.

I ran into an issue during my latest project however where JW Player was working fine in Firefox but on Internet Explorer I was getting a blank area where the video should have been appearing.

The Solution
It turned out the path to my swfobject.js was not valid. I was previously using:

<script src='/js/swfobject.js' type='text/javascript'></script>

In order to get the player to work in all browers I needed to change my code to either:

<script src='js/swfobject.js' type='text/javascript'></script>

Or:

<script src='http://www.mydomain.com/js/swfobject.js' type='text/javascript'></script>

See how the path to ‘swfobject.js’ is either relative or absolute, rather than referring to the root? After making the above amendments it worked perfectly across all browsers.

Share