PHP doesn't have a music player built in, but luckily there's a 22k dos file that can play midis, mp3s and wavs making for the perfect jukebox.
Just for practice. Many people only know php and making a music player is cool.
First we have to make something to scan for music. Basically the code will scan all the files in a folder.
The open dir function is the one to use to read the contents of the folers. The documentation page provides a functional copy and paste example on reading a folders content. We'll just modify it.
Substitute x for whatever directory you keep your music in.
<? $sheets=Array();
$dir = "x";
$dh = opendir($dir) or die ("could not open dir");while ( !(($file = readdir($dh)) === false) ){
if ($file == "." || $file == "..") continue;
if (eregi("(.mp3|.mid|.wav)$",$file)){
$sheets[] .= $file;
}
}
closedir($dh);
?>The opendir function puts the contents of the folder into and array and passes it to $dh. Then we pass the $dh through a while loop and make sure all files are readable. It then says to ignore the . and .. options about relative folder path. You can delete that part in this case. Next, the eregi function says, if it finds .mp3, .mid or .wav at the end of the file put it into the array $sheets.
You can print the contents of an array with the print_r() function so add print_r($sheets); to the end of your array to see all listings. I want a random song though. Lets use the function array_rand, which returns an index of the chosen array.
echo array_rand($sheets);
This gives us a random number representing the index, to get a song we have to call that index from the array.
$randomsong=$sheets[array_rand($sheets)]; echo $randomsong;
To play the song we need to get a command line player. (Download wav.exe a dos mp3 wav and midi player. We could also use the html embed code with dhtml (divid.innerHTML javascripts) to play songs without refreshing the page, but this tutorial focuses on DOS.
system("C:/blah/blah/WAV.EXE $dir/$randomsong");Get the location of your wav.exe then the location of the songs you ave your folders in along with the random song we got above. It should play.
That's it really. Getting the exec or system function to work will be the hardest part probably. Then just loop it with a while loop. I've added silly for loops redundantly echoing rn just to mimic some kind of cls clear screen. The sleep is so people can break the code since it's an infinite loop. Ctrl+C is a dos termination sequence to quit the loop. Escape is a built-in function in wav.exe and will terminate the song.
Here's mine. It's just an mp3 player since I dont have wavs or midis.
<?
while(true){
for ($i=0;$i<20;$i++){
echo "rn";
}
echo "CTRL+C is the dos escape sequencernrn";
sleep(5);
$sheets=Array();
$dir = "C:phptest";
$dh = opendir($dir) or die ("could not open dir");
while ( !(($file = readdir($dh)) === false) ){
if ($file == "." || $file == "..") continue;
if (eregi(".mp3",$file)){
$sheets[] .= $file;
}
}
/* close the directory */
closedir($dh);
$foo=$sheets[array_rand($sheets)];
echo "Now playing $foo";
for ($i=0;$i<3;$i++){
echo "rn";
}
exec("C:phptestWAV.EXE C:phptest".$foo." /Q");
for ($i=0;$i<15;$i++){
echo "rn";
}
}
?>
Checkout my Mp3 and files search that uses google to find music/albums, anime, games and movies with direct downloads. It's very safe and easy to use, just search for a song, then right click save as to download it.
See my new funny pictures blog. I accidently deleted some of the old blogging software which I made, so I just rewrote everything from scratch. This blog won't be updated anymore =/.
I make video tutorials on a variety of topics on youtube. Please help me out by adding me as a friend if you have a youtube account. It really helps.
Clicking the links above will give you a random but extremely high rated video. There are over 15,000 awesome videos indexed so click as many times as you'd like.
Save To Del.icio.us,
Submit to Digg,