New here? Then you may want to subscribe to my rss feed. :)

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:

  1. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
  2. codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8"
  3. width="320" height="20"
  4. id="randomplayer">
  5.  
  6.         <param name=movie value="mediaplayer.swf">
  7.         <param name=quality value="high">
  8.         <param name=bgcolor value="#FFFFFF">
  9.         <param name=allowFullScreen value="false">
  10.         <param name=swLiveConnect value="true">
  11.         <param name=allowScriptAccess value="sameDomain">
  12.         <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
  13. &autostart=true&backcolor=0xFFFFFF">
  14.  
  15.         <embed type="application/x-shockwave-flash"
  16.         pluginspage="http://www.macromedia.com/go/getflashplayer"
  17.         width="320" height="20" bgcolor="#FFFFFF"
  18.         name="randomplayer"
  19.         src="mediaplayer.swf"
  20.         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">
  21.         </embed>
  22.  
  23. </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.

13 Responses to “Play a random mp3 file from a specific directory”

  • Gravatar
    Josh Location: United States //

    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

  • Gravatar
    Josh Location: United States //

    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

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    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.

  • Gravatar
    Josh Location: United States //

    Hmm. I can’t figure it out.

    The player will have to do as is for now.

    Thanks again for your help.

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    No worries. If you need it adding then let me know.

  • Gravatar
    Josh Location: United States //

    I do, but I’ve already taken a lot of your time. I’ll twiddle with it when I get a chance.

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    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>

  • Gravatar
    Josh Location: United States //

    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

  • Gravatar
    Josh Location: United States //

    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

  • Gravatar
    Fiaz Location: Great Britain (UK) //

    I had a quick look, and made some changes. Still no joy. When i get a chance i will take another look.

  • Gravatar
    Josh Location: United States //

    Sounds fantastic, thank you.

  • Gravatar
    Josh Location: United States //

    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…

  • Gravatar
    jameswillisisthebest Location: United States //

    This is my first post
    just saying HI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>