Приглашаем посетить
Спорт (sport.niv.ru)

Hack 96. Create Dynamic Playlists

Previous
Table of Contents
Next

Hack 96. Create Dynamic Playlists

Hack 96. Create Dynamic Playlists Hack 96. Create Dynamic Playlists

Use the XML Simple Playlist Format (XSPF) to create playlists from PHP.

Creating an MP3-playing application for you and your friends is easy with PHP. A new standard for playlists called XSPF (http://www.xspf.org/) is available and can be used by a Flash MP3 player movie called Music Player (http://musicplayer.sf.net). And, of course, PHP will work with all of this.

Figure 10-6 illustrates how the Flash movie requests the XSPF playlist XML from the playlist.php script.

Figure 10-6. The Flash movie requesting the playlist from the PHP script
Hack 96. Create Dynamic Playlists


10.3.1. The Code

Save the code in Example 10-2 as index.html.

Example 10-2. Beginning the process of setting up an MP3 player
<html>
<body>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.
cab#version=7,0,0,0"
  width="400" height="153" id="xspf_player" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="http://localhost/xspf/xspf_player.
swf?autoload=true&playlist_url=http://localhost/xspf/playlist.php" />
<param name="quality" value="high" />
<param name="bgcolor" value="#e6e6e6" />
<embed src="http://localhost/xspf/xspf_player.swf?autoload=true&playlist_
url=http://localhost/xspf/playlist.php"
  quality="high" bgcolor="#e6e6e6" width="400" height="153"
  name="xspf_player" align="middle" allowScriptAccess="sameDomain"
  type="application/x-shockwave-flash"
  pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</body>
</html>

Now save the code in Example 10-3 as playlist.php.

Example 10-3. A little more PHP for creating an XML playlist
<? echo( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" ) ?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<title>My Radio</title>
<trackList>
<?
$dir = opendir( "." );
while ($file = readdir($dir)) {
if ( preg_match( "/[.]mp3$/i", $file ) ) {
?>
<track>
<location>http://localhost/xspf/<? print($file) ?></location>
<annotation><? print( $file ) ?></annotation>
</track>
<? } } ?>
</trackList>
</playlist>

10.3.2. Running the Hack

Upload the PHP and HTML from the preceding code, upload the XSPF Flash movie from the Music Player site (http://musicplayer.sf.net/) into the same directory, and upload any MP3 files you want into the same directory. Then navigate to that page with your web browser.

Hack 96. Create Dynamic Playlists

The directory used in this example is xspf. If you change the name of the directory, you will need to change the references in the index.html file.


You should see something similar to Figure 10-7.

Figure 10-7. The Flash MP3 player showing the playlist
Hack 96. Create Dynamic Playlists


The files you see in the player will be the MP3 files in the same directory as the PHP script. The script looks through the directory, finds the MP3 files, and adds them to the XSPF playlist file.

This can be a very handy script if you are podcasting and you want a page that has a Flash player with all of your blogs. As an example, Figure 10-8 shows the player being used on artist Joshua Armstrong's site (http://joshuaarmstrong.net/).

Figure 10-8. Flash player in use on Joshua Armstrong's site
Hack 96. Create Dynamic Playlists


The player is integrated into the home page in the lower-righthand corner over a background image. That presents an all-in-one package: the artist's image, a place to buy the album, and the music from the album, all on the home page.

10.3.3. See Also

  • "Create a Media Upload/Download Center" [Hack #97]


Previous
Table of Contents
Next