Jump to content

Player Urgent


ben3001

Recommended Posts

Hi Guys long time,I have a little problem here i have a project in for tomorrow and my group have let me down.I need a music player that loads all mp3 files that are in a folder.I have seen some where you have to specify the folder you want in the place list (in the xml file), but this is not what i need. It has to display all the files in that folder.Im really a little anoyed becuase at the last minute my mates said they couldnt get anything to work.Any help is really appreacited This is one examplehttp://www.premiumbeat.com/flash_resources...player_menu.phpBut you have to have direct url, as shown in the below xml file

<?xml version="1.0" encoding="UTF-8"?><xml>	<track>		<path>samples/</path>		<title>Track 1 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 2 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 3 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 4 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 5 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 6 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 7 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 8 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 9 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 10 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 11 - Track Title</title>	</track>	<track>		<path>song.mp3</path>		<title>Track 12 - Track Title</title>	</track></xml>

Ben

Link to comment
Share on other sites

The Flash player can't read which files are in a folder. You'll need to give it a list of files somehow. If you want it to be dynamic then use a PHP script for the playlist that can read the filenames and build an XML playlist with everything in the folder. But Flash alone can't enumerate a folder's contents.

Link to comment
Share on other sites

You can use the scandir function to list all files and directories:http://www.php.net/manual/en/function.scandir.phpWhen you loop through the array that scandir returns you can use is_dir to check if it's a file or a directory:http://www.php.net/manual/en/function.is-dir.phpYou'll only want to include the files. In the loop you can have the script output each file in an XML format that the Flash player can read. There might be a way with PHP to read the track name from the ID3 information in the MP3 file but I'm not aware of any. You might just use the filename for the track name as well.Just kidding, there's this:http://www.php.net/manual/en/ref.id3.php

Link to comment
Share on other sites

Hey mate thanks for the quick reply, how would i go about this?Tonight is really my last chance
Do you need to fetch any more detailed info about the MP3, or is getting the name (without the extension) enough?If you do want something more sophisticated, I hope you've estabilished a naming policy on the MP3s, 'cause getting the ID3 info would be a ######.[edit]Oh. I see justomeguy posted right before me with an easy solution for that part. Oh well... replace "basename($song)" with whatever you want to appear as a title, and you're all set[/edit]Here's a sample code that should work, provided you have PHP5:
<?phpheader('Content-type: application/xml');$dom = new DOMDocument('1.0', 'UTF-8');$dom->appendChild($dom->createElement('xml'));$root = $dom->documentElement;$songs = array_splice(scandir('/mp3s/'), 2);foreach ($songs as $song) {$track = $dom->createElement('track');$track->appendChild($dom->createElement('path', $song));$track->appendChild($dom->createElement('title', basename($song)));$root->appendChild($track);}echo $dom->saveXML();?>

(replace the string '/mp3s/' with the actual location of the MP3s)Access that file and it will generate a list like the one you've described, but keep in mind the title seen will be the same as the filename with no extension.P.S. I'm sorry, but your toast if you have PHP4[edit]@justsomeguy, I've now added array_splice() to adress this issue. Thanks for the reminder.[/edit]

Link to comment
Share on other sites

If you have no need to modify an existing playlist then there's no point in adding the complexities of SimpleXML or DOMXML. I simply took an existing script that I use to generate html code (<img ... />) for galleries dynamically from directories of images and adapted it to generating a playlist. The format for playlist I used is XSPF because it's widely supported (online and offline), I know where to get a very simple and easy flash mp3 flash player, it's all open source, and it's a standard xml specification for media playlists. For information on the XSPF spec go here.http://www.xspf.org/quickstart/For the embedded flash mp3 player that reads the xspf files go here:http://musicplayer.sourceforge.net/#downloadHere is the code that reads a directory and outputs an XSPF file.

<?php$dir = "files";  //Specifies the directory where files will be loaded from$dh = opendir($dir);$count = 0;//Cycles through the directory and loads the the mp3List array with trackswhile (false !== ($file = readdir($dh))) {			//Skips the '.' and '..' items of the directory listing			if ($file != "." && $file != "..") {				$isMp3 = explode('.',$file,2);				// Loops through only the files with a .mp3 extension				if ($isMp3[1] == "mp3") {					$count += 1;					$mp3List[$count] = $file;				}			}}closedir($dh);//The next part takes the filelisting and generates an XSPF file$File = "playlist.xspf";$fh = fopen($File, 'w') or die("can't open file");$stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";fwrite($fh, $stringData);$stringData = "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n";fwrite($fh, $stringData);$stringData = "<trackList>\n";fwrite($fh, $stringData);foreach ($mp3List as $track) {	$stringData = "<track>\n";	fwrite($fh, $stringData);	$stringData = "\t<title>$track</title>\n";	fwrite($fh, $stringData);	$stringData = "\t<location>$dir/$track</location>\n";	fwrite($fh, $stringData);	$stringData = "</track>\n";	fwrite($fh, $stringData);}$stringData = "</trackList>\n";fwrite($fh, $stringData);$stringData = "</playlist>\n";fwrite($fh, $stringData);fclose($fh);?>

Shortcomings: 1: The spaces in the xspf file need to be converted to %20. This can be done using preg_replace. I would do it myself but I haven't figured out how to use regular expressions yet2: It only loads the playlist from one directory. This script could be extended to fish through subdirectories too with a little php-fairy-dust.Sidenote: I've been looking for an excuse to write one of these for a while now. It was a good challenge and I hope it fits your needs.

Link to comment
Share on other sites

Here... I tried to validate the XSPF output and found out that the file location needs to be a valid URI. This version adds a root directory source that's tacked to the location URI, and it uses preg_replace to convert spaces to %20. If you want to add more preg replaces I made the find and replace arrays for extensibility. For a resource of invalid chars in the URI go here:http://www.blooberry.com/indexdot/html/top...urlencoding.htmHere is the tested and working code.

<?php$root = "http://testing.jpasims.net/mp3";$dir = "files"; //Specifies the directory where files will be loaded from$playlist_name = "playlist.xspf";$dh = opendir($dir);$count = 0;//Cycles through the directory and loads the the mp3List array with trackswhile (false !== ($file = readdir($dh))) {			//Skips the '.' and '..' items of the directory listing			if ($file != "." && $file != "..") {				$isMp3 = explode('.',$file,2);				// Loops through only the files with a .mp3 extension				if ($isMp3[1] == "mp3") {					$count += 1;					$mp3List[$count] = $file;				}			}}closedir($dh);//The next part takes the filelisting and generates an XSPF file$fh = fopen($playlist_name, 'w') or die("can't open file");$stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";fwrite($fh, $stringData);$stringData = "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n";fwrite($fh, $stringData);$stringData = "<trackList>\n";fwrite($fh, $stringData);foreach ($mp3List as $track) {	$stringData = "<track>\n";	fwrite($fh, $stringData);	$stringData = "\t<title>$track</title>\n";	fwrite($fh, $stringData);		$URI = "\t<location>$root/$dir/$track</location>\n";	// Set to replace any characters that may make the uri invalid	$find = array('/ /');	$replace = array('%20');	$stringData = preg_replace($find, $replace, $URI);	fwrite($fh, $stringData);		$stringData = "</track>\n";	fwrite($fh, $stringData);}$stringData = "</trackList>\n";fwrite($fh, $stringData);$stringData = "</playlist>\n";fwrite($fh, $stringData);fclose($fh);?>

If you wanted to incorporate both this and the mp3 player on the same page just embed the player at the top, create a little front-end to generate the playlist and waam baam thank you maam, you're done.Enjoy

Link to comment
Share on other sites

I had a chat with Ben last night, shortly after my last post submission.I think we resolved the issue by using DOMXML. His server had (as I suspected) PHP4, but fortunatly, it had the DOMXML extension, so I rewrited my code above to use DOMXML instead of DOM.Scandir we got from PEAR's PHP_Compat.It works well now. At least the part for the XML generation that is. It was too late at night already, so I didn't stayed to see how it went with the player.Ben, any luck?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...