Random song generator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JamesMLena
Forum Newbie
Posts: 2
Joined: Sun Nov 07, 2004 5:40 pm

Random song generator

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
JamesMLena
Forum Newbie
Posts: 2
Joined: Sun Nov 07, 2004 5:40 pm

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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]";
Post Reply