Forum Moderators: coopster

Message Too Old, No Replies

Dynamically Load mp3 files

         

lonfipe

5:35 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Hello,

I have a media player with the following parameters:

----------------------------------------------
<script type='text/javascript'>
var so = new SWFObject('myplayer.swf','ply','300','28','9','#ffffff');
so.addParam('allowfullscreen','true');
so.addParam('allowscriptaccess','always');
so.addParam('wmode','opaque');
so.addVariable('file','audio/asong.mp3');
so.write('playerbox');
</script>
------------------------------------------------

That works fine for one mp3 file.

How do i dynamically load mp3 files into the media player?

I would like to load all my mp3 files into the folder "audio" on the website.

I would then like to be able to link to any of those files (click to listen) and when someone clicks on the link, the mp3 (for example - awesome.mp3) plays via the media player.

The site is on a MySQL database.

I am learning php but am not yet able to do stuff like this.

Thanks.

d40sithui

7:47 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Hi lonfipe,
Welcome.
The syntax here is simple. You would want to focus on the so.addVariable('file','audio/asong.mp3');
You would write a PHP loop to read all the contents of /audio directory and retrieve all the .mp3 files and store them in strings, or an array.
So here is the something to work with::


<script type='text/javascript'>
var so = new SWFObject('myplayer.swf','ply','300','28','9','#ffffff');
so.addParam('allowfullscreen','true');
so.addParam('allowscriptaccess','always');
so.addParam('wmode','opaque');

//begin php code here
<?
//open directory of mp3 files
//while dir is open, retrieve all files ending in .mp3.
//echo "so.addVariable('file', $fileName)";
?>

so.write('playerbox');
</script>

This method is simplest, however, if you have a lot of mp3s, the view source would return a super long html. The more practical solution (if you have a lot of mp3s to load) is to instead load a playlist instead of the each individual mp3. Good luck!

rocknbil

10:24 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem with that is you are indeed reading all the files, but each one is overwriting the variable "file" with the next file name. So only the last one will be imported into the player.

To do this, you are going to need to modify the Flash player itself so is supports a "playlist." By the looks of it, this player only imports one at a time.

You could devise a series of links, that when clicked change the value of "file," but a more stable method would be to modify the player. Then you'd addVariable('music-directory'.... and the flash could build an internal list. Or you could use XML methods, but at any rate a single-item player can't do this without modification, or by the link method mentioned above.