Play a random mp3 file from a specific directory
June 4th, 2007 | Posted in Development, PHP, Wordpress | 13 Comments
In this post i discussed how to play random media files using wordtube by passing a comma delimited list of media ids.
Josh asked if there was a way of playing files from a specific directory rather than having to upload through wordtube and listing all media file ids.
Having helped Josh I thought it would be of use to others if I posted a little how to.
Firstly, you will need to download, install and activate coffee2code Random File plugin. This will randomly select a file from the specified directory and return it as a url ready to pass into the flash player.
Next, you need to download Flash Mediaplayer and upload mediaplayer.swf. For the sake of this how-to, I have uploaded it to the web root.
You will need to create the code to embed the flash player. Fortunately there is a wizard available here which will create all the code for you.
Copy paste the generated embed code and replace the file parameter value with a call to the random file plugin like so:
-
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
-
codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8"
-
width="320" height="20"
-
id="randomplayer">
-
-
<param name=movie value="mediaplayer.swf">
-
<param name=quality value="high">
-
<param name=bgcolor value="#FFFFFF">
-
<param name=allowFullScreen value="false">
-
<param name=swLiveConnect value="true">
-
<param name=allowScriptAccess value="sameDomain">
-
<param name="FlashVars" value="file=<?php echo c2c_random_file(’/path_to_music_folder_relative_from_webroot’, ‘mp3′, ‘url’); ?>& width=320&height=20&displaywidth=320&displayheight=0
-
&autostart=true&backcolor=0xFFFFFF">
-
-
<embed type="application/x-shockwave-flash"
-
pluginspage="http://www.macromedia.com/go/getflashplayer"
-
width="320" height="20" bgcolor="#FFFFFF"
-
name="randomplayer"
-
src="mediaplayer.swf"
-
flashvars="file=<?php echo c2c_random_file(’/path_to_music_folder_relative_from_webroot’, ‘mp3′, ‘url’); ?>&width=320&height=20&displaywidth=320&displayheight=0 &autostart=true&backcolor=0xFFFFFF">
-
</embed>
-
-
</object>
Taken from plugin authors page:
The directory of random files must exist at the directory structure level of your WordPress installation or below. (i.e., if your site is installed on your server at /usr/local/htdocs/yoursite/www/journal/, then the directory of random files you specified will assume that as its base… so $dir=’randomfiles’ would be assumed to actually be: /usr/local/htdocs/yoursite/www/journal/randomfiles/)
Leading and trailing ‘/’ are unnecessary… ‘/randomfiles/’ == ‘/randomfiles’ == ‘randomfiles/’ == ‘randomfiles’
Finally, you need to place the code in one of your theme templates. Hopefully, you will have the player stream a random mp3 from a specified directory on every page load. If you encounter any problems then ask.












This works fantastic, thank you very much!
My next task will be to get the mediaplayer to display the .mp3 track title…
Cheers! And once more, thank you.
josh
Hi again,
One quick question, for displaying the song title I found this: http://www.jeroenwijering.com/?thread=5083
But I cannot seem to find the line that the thread is referring to…
Any thoughts?
Thanks,
Josh
Its giving you several ways to display the track title, to be honest I think hacking the fla and acrionscript will be a bit beyond you.
This page allows you to display the track title using javascript.
Probably the easier option.
Hmm. I can’t figure it out.
The player will have to do as is for now.
Thanks again for your help.
No worries. If you need it adding then let me know.
I do, but I’ve already taken a lot of your time. I’ll twiddle with it when I get a chance.
Try adding this to just above (or below) the player:
<script type=”text/javascript”>
function itemData(obj) {
var nodes = “”;
nodes += “Now Playing: “+obj['title']+”";
document.getElementById(”data”).innerHTML = nodes;
};
</script>
<div id=”data”></div>
Thanks! Here’s what I have, but I think that I’ve placed the code in the wrong place:
Random local indie audio tracks sponsored by the above advertiser:
&autoscroll=true&image=&overstretch=true&showdigits=true&callback=&backcolor=0×2f2f2f&frontcolor=0xffffff&lightcolor=0xffff00&volume=100&bufferlength=5&width=320&height=40&showfsbutton=false”
wmode=”transparent”
quality=”high”
bgcolor=”#FFFFFF”
name=”2″
id=”2″
style=”"
src=”http://www.thestonewailer.com/wp-content/mediaplayer.swf”
type=”application/x-shockwave-flash”
/>
function itemData(obj) {
var nodes = “”;
nodes += “Now Playing: “+obj[’title’]+””;
document.getElementById(”data”).innerHTML = nodes;
};
Archives
Categories
RSS
“>Entries RSS
“>Comments RSS
I think I understand:
Did you mean to add that snippet of code to the sidebar.php or some other place? I have it in the sidebar, just below the player code, but to no avail…
js
I had a quick look, and made some changes. Still no joy. When i get a chance i will take another look.
Sounds fantastic, thank you.
Guess what: I found this http://www.coolcode.cn/?p=100 to be very helpful in displaying track titles.
It uses these [coolplayer]yourlinkhere[/coolplayer] tags to call media files. I’m currently using it in my latest post on http://www.thestonewailer.com to play .m3u files from a third-party site.
I’ll need to play around with it some more, but it might do the trick for the sidebar random audio problem…
This is my first post
just saying HI