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:
-
[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.
-
[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
-
$search = "/\[MEDIA=(\d+)\]/"; //search for ‘media’ entry
-
-
foreach ($matches[1] as $content_id) {
-
-
$search = "[MEDIA=".$content_id."]";
-
-
$dbresult = $wpdb->get_results("SELECT * FROM $wpdb->wordtube WHERE vid = ‘$content_id’");
-
if ($dbresult) {
-
$replace = replacevideo($dbresult[0]->vid, $thisplayer ,"single");
-
}
-
}
-
}
-
-
return $content;
-
} // end search content
New Version
-
$search = "/\[MEDIA=(\d+,?)*\]/";
-
foreach ($matches[0] as $content_id) {
-
$search = ‘[MEDIA='.$content_id.']‘;
-
if(stripos($content_id, ‘,’)){
-
$content_id = $content_id_array[$random_number];
-
}
-
$dbresult = $wpdb->get_results("SELECT * FROM $wpdb->wordtube WHERE vid = ‘$content_id’");
-
if ($dbresult) {
-
$replace = replacevideo($dbresult[0]->vid, $thisplayer ,"single");
-
}
-
}
-
}
-
return $content;
-
} // end search content
If you encounter any problems then let me know.












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
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. ;)
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”
/>
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
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
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.
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
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
Try the following:
<?php echo c2c_random_file(’/localtrax’, ‘mp3′, ‘url’); ?>
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
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.
[...] More … [...]