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

How to play media files at random using Wordtube wordpress plugin

March 28th, 2007 | Posted in Development, Wordpress | 12 Comments

Wordtube is a brilliant wordpress plugin that allows you to seamlessly play media files using Flash Media Player. The plugin has its own media manager where you can create playlists, set player parameters and upload media files.

I uploaded several mp3 files which I wanted to play, problem was I couldn’t decide which one to play. Instead of creating a playlist i wanted to load tracks at random.

To achieve this i had to hack the code which parsed and played the media file to accept a comma delimited list of media file id’s.

To embed a media file you uploaded the mp3, and embed the following code in the body:

  1. [MEDIA=ID_OF_MEDIA_FILE]

The new version now accepts as well as a single media file id, a comma delimited list of media file id’s.

  1. [MEDIA=11,12,14,17]

When the page loads it randomly selects an id to play.

The code to change can be found in wordtube.php on lines 107 to 124 (or thereabouts). Make sure you backup your file before making any changes.

Original Code

  1. $search = "/\[MEDIA=(\d+)\]/";   //search for ‘media’ entry
  2. preg_match_all($search, $content, $matches);
  3.  
  4. if (is_array($matches[1])) {
  5.         foreach ($matches[1] as $content_id) {
  6.  
  7.                 $search = "[MEDIA=".$content_id."]";
  8.  
  9.                 $dbresult = $wpdb->get_results("SELECT * FROM $wpdb->wordtube WHERE vid = ‘$content_id’");
  10.                 if ($dbresult) {
  11.                         $replace = replacevideo($dbresult[0]->vid, $thisplayer ,"single");
  12.                         $content = str_replace ($search, $replace, $content);
  13.                         }
  14.                 }
  15.         }
  16.  
  17.         return $content;
  18. } // end search content

New Version

  1. $search = "/\[MEDIA=(\d+,?)*\]/";
  2. preg_match_all($search, $content, $matches);
  3. if (is_array($matches[0])) {
  4.         foreach ($matches[0] as $content_id) {
  5.                 $content_id = ereg_replace(\[MEDIA=(.*)\],\1,$content_id);
  6.                 $search = ‘[MEDIA='.$content_id.']‘;       
  7.                 if(stripos($content_id, ‘,’)){
  8.                         $content_id_array = explode(‘,’,$content_id);
  9.                         srand ((double) microtime( )*1000000);
  10.                         $random_number = rand(0,count($content_id_array)-1);
  11.                         $content_id = $content_id_array[$random_number];
  12.                 }
  13.                 $dbresult = $wpdb->get_results("SELECT * FROM $wpdb->wordtube WHERE vid = ‘$content_id’");
  14.                 if ($dbresult) {
  15.                         $replace = replacevideo($dbresult[0]->vid, $thisplayer ,"single");
  16.                         $content = str_replace ($search, $replace, $content);
  17.                         }
  18.                 }
  19.         }
  20.         return $content;
  21. } // end search content

If you encounter any problems then let me know.

12 Responses to “How to play media files at random using Wordtube wordpress plugin”

  • Gravatar
    Josh Location: United States //

    Fantastic addition to the code!

    Any idea how I can make this call [MEDIA=11,12,14,17] play random audio files from my MEDIA directory without having to plug in the track numbers? Maybe by calling random file names from the dir?

    I am thinking that a combination of your altered version of the wordtube script plus Scott Reilly’s random file plugin (http://www.coffee2code.com/archives/2004/07/08/plugin-random-file/)

    Might do the trick…

    Thanks,
    Josh

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

    I think it would take some real hacking to get wordtube to do what you want. WT queries the database and returns an xml structure of file data. Would take less work to write your own than to bend WT into shape.

    Unless there is another plugin that does this, i reckon this sounds like a nice little plugin that needs developing. ;)

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

    This is totally untested but “looks” like it might work:

    1. Install the coffeecode random file plugin.
    2. If you don’t have wordtube then install the flash player itself and change the path to it

    Use this to play the file:

    <embed
    width=”320″
    height=”20″

    flashvars=”file=http://www.mywebsite.com/< ?php echo c2c_random_file('/path/to/music/folder/'); ?>&
    image=&
    overstretch=true&
    showdigits=true&
    callback=&
    backcolor=0×2f2f2f&
    frontcolor=0xffffff&
    lightcolor=0xffff00&
    volume=80&
    bufferlength=5&
    width=320&
    height=20&
    showfsbutton=false”

    wmode=”transparent”
    quality=”high”
    bgcolor=”#FFFFFF”
    name=”2″
    id=”2″
    style=”"
    src=”http://www.mywebsite.com/wp-content/plugins/wordtube/mediaplayer.swf”
    type=”application/x-shockwave-flash”
    />

  • Gravatar
    Josh Location: United States //

    OK, so it “wants” to work. Here’s what I have for code:

    &image=&overstretch=true&showdigits=true&callback=&backcolor=0×2f2f2f&frontcolor=0xffffff&lightcolor=0xffff00&volume=80&bufferlength=5&width=320&height=20&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”
    />

    But, if you check out http://www.thestonewailer.com, you’ll see parse errors on the right near the top of the sidebar where I’m trying to call the audio files…

    Any tips? It is calling the files randomly, and if I use the “echo” command I get the mediaplayer, but no files, so I have to use the “include” command, which I learned after using the coffe2code a few weeks ago to call random text and ads from a directory.

    Thanks!
    Josh

  • Gravatar
    Josh Location: United States //

    Hi again,

    Here are three screen shots as examples of what I was talking about. The first two show parse errors that pop up when I use “include” to call the file with coffee2code, which is how I do it elsewhere on the site.

    The last one is a shot of the mediaplayer, so I know that that part is working OK, it just isn’t getting the files correctly.

    Any thoughts?

    Cheers!
    Josh

    http://i78.photobucket.com/albums/j119/noeticcommunications/stonesampleone.jpg?t=1180891837
    http://i78.photobucket.com/albums/j119/noeticcommunications/stonesampletwo.jpg?t=1180891878
    http://i78.photobucket.com/albums/j119/noeticcommunications/stonesamplethree.jpg?t=1180891912

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

    Try changing the reftype coffeecup plugin argument to url. The problem is that it needs the url to the mp3 rather than the system file path.

  • Gravatar
    Josh Location: United States //

    Gotcha, but I can’t seem to find that line in the code. Here’s a link to a copy of the code. Do you see it in there?

    http://www.thestonewailer.com/coffecodetest.txt

    Thanks, sorry I’m such a hassle,
    Josh

  • Gravatar
    Josh Location: United States //

    Ok, here’s what I’ve tried (thanks for the ‘url’ tip that keeps parse errors from popping up), but it won’t grab a file. The mediaplayer just churns away trying to load, or displays without an audio file:

    flashvars=”file=http://www.thestonewailer.com/
    ******
    flashvars=”file=http://www.thestonewailer.com/
    ***********
    flashvars=”file=http://www.thestonewailer.com/
    *********
    flashvars=”file=http://www.thestonewailer.com/

    Any ideas?

    Cheers!
    Josh

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

    Try the following:

    <?php echo c2c_random_file(’/localtrax’, ‘mp3′, ‘url’); ?>

  • Gravatar
    Josh Location: United States //

    Hmmm. No parse errors, and the mediaplayer looks like it is trying to load a file, but it isn’t loading anything…

    http://www.thestonewailer.com

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

    Should be working ok now on your site.

    I have created a post detailing what you need to do if anyone else requires something similar.

  • Gravatar
    Wordpress » Blog Archive » play media files at random Location: United States //

    [...] More … [...]

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>