Page 1 of 1

Random song generator

Posted: Sun Nov 07, 2004 5:45 pm
by JamesMLena
I have a script that will randomly pick a song file from the selected folder: http://www.jameslena.com/blog/songrotate/songrotate.php And I have a embed tag with the .php as the file being played. So the php gets the file, and the embed plays the file that it gets from the php. This is set up at http://www.jameslena.com/blog all the way at the bottom.

I want to know how to get it to to display the file name in text under the player. I don't know PHP at all so I don't know how to code it how I want. I think u could write a line that has it extract the $file from the other php script. Something like /folder/random.php get file=$file, but in PHP.

Any help?

Posted: Sun Nov 07, 2004 6:37 pm
by John Cartwright
Show us what you have so far. It is impossible to tell you what you want without knowing how you have things setup.

Posted: Sun Nov 07, 2004 6:56 pm
by JamesMLena
<?php
/*
By James Lena > http://jameslena.com
Inspired by Matt Mullenweg > http://photomatt.net
*/

$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'mp3 wav';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

Thats the random php script.



And I have the embed setup like:

<embed src="/blog/songrotate/songrotate.php" width="400" height="45" autostart="true" loop="false">


And by the way I have just changed the blog so the song doesn't play anymore, but if anyone can please help me so I can use it again.

Posted: Sun Nov 07, 2004 10:56 pm
by kettle_drum
Well as long as you give the file name a standard naming format that includes the name and artist then you can just parse the file name to get the data.

Code: Select all

echo "Now playing: $files[$rand]";