Programming Programming
Google Teaches MySQL
PHP Jukebox Tutorial
Clear Screen in PHP
Rebuilding Apache Logs
Zipping Files with PHP
Del.icio.us Query String Hack
PHP Blogging Tricks
PHP link validator
Google Video for Programming
Newby Webmaster Tips
Cool Pictures All Categories
Internet Videos [ 576 x RSS ]
Front Page [ 118 x RSS ]
Cool Pictures [ 57 x RSS ]
Cool Sites [ 55 x RSS ]
Video Tutorial [ 19 x RSS ]
My Websites
Best of The Internet
Free Video Tutorials
Best of Youtube
Use Google to Download mp3s
Free Quiz Creator
Online Education
Poetry
Famous Poetry
Printable Sheet Music
JimmyR on Youtube
Free Movies Online

Saturday, 26th of January 2008 (26-01-2008)

Howto Make a PHP DOS MP3 Player

PHP Tutorial Overview

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.

Why the Hell Would you Use PHP!?

Just for practice. Many people only know php and making a music player is cool.

Scanning Files

First we have to make something to scan for music. Basically the code will scan all the files in a folder.

Opendir()

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.

Scanning for Music

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.

Lets Grab the Filename of a Random Song

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;

Playing the Random Song

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.

Complete Code

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";
}
}
?>

Download mp3s Faster than limewire free!

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.

My new Blog

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 =/.

Free Video Tutorials

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.

Free TV from Youtube!

Random: Cute Pets | Video Tutorial | TV | Funny | Anime

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, Stumble it!


Share This Page


Table of Contents

  1. Howto Make a PHP DOS MP3 Player

  2. PHP Tutorial Overview
  3. Why the Hell Would you Use PHP!?

  4. Scanning Files
  5. Opendir()
  6. Scanning for Music
  7. Lets Grab the Filename of a Random Song
  8. Playing the Random Song

  9. Complete Code

Featured on JimmyR.com

Funny Test Answers
[comic] Bunny Suicides
Baby Can't Stop Laughing
Free Movies Online
Video Game Time Attacks
Cool Magic Videos
Picture Everyday Videos
Funny Cat Videos
Complete List of Videos

Add me On Youtube

Add me

My Video Tutorials

Youtube Video Tutorials

Google