Creating an XSPF playlist automatically

Joined: 11/28/2008

Hey everyone,

OK so as the topic suggests... I am working on automatically creating an XSPF playlist for an open source player.

This is what I have thus far:

index.htm

<html>
<head>
<title>Click to launch the MP3 player</title>
</head>
<body>
Press the button to launch the MP3 Player.<br />
The player will scan the 'mp3' folder and sub-folders for MP3s to auto-create a playlist.
<form name="create_playlist" method="post" action="playlist.php">
<input type="submit" value="Create Playlist" />
</form>
</body>
</html>

playlist.php

<?php
 
//-----------------------------------------------------------------
//				Search for MP3s and their Album Art
//						*** requires PHP5 ***
//-----------------------------------------------------------------
 
// MP3 extension
$mp3 = ".mp3";
 
// Album Art Filename (Has to be the same in each folder that the MP3s are found.)
$albumart = AlbumArtSmall.jpg;
 
// Path to the root folder that you want to scan
$directory = "/mp3";
 
$it = new RecursiveDirectoryIterator( new RecursiveDirectoryIterator($directory));
 
foreach(new RecursiveIteratorIterator($it) as $file){
  if (!((strpos(strtolower($file), $mp3)) === false)){
    $items[] = preg_replace("#\\\#", "/", $file);
    $image = $directory.$albumart;
  }
}
sort($items);
 
// header("content-type:text/xml;charset=utf-8");
 
$filename=fopen('playlist.xspf',"w");
$contents = "<?xml version='1.0' encoding='UTF-8'?>\n
<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n
	<title>PHP Generated Playlist</title>\n
	<info>http://www.jeroenwijering.com/</info>\n\n
<trackList>\n
 
foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  $albumart = 
  $image = file_exists($albumart)
 
	<track>\n'
		<annotation>'.$title.'</annotation>\n
		<location>'.$item.'</location>\n
		<image>'.$image.'</image>\n
	</track>\n';
}
 
</trackList>\n
</playlist>\n";
 
fwrite($filename,utf8_encode($contents));
fclose($filename);
 
	echo "< script >window.open('player.htm')< /script >";
?>

player.htm

<html>
<head>
<title>MP3 Player</title>
</head>
<body>
 
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"
width="400" height="168">
	<param name=”allowScriptAccess” value=”sameDomain”/>
	<param name=”movie” value=”xspf_player.swf”/>
	<param name=”quality” value=”high”/>
	<param name=”bgcolor” value=”#E6E6E6″/>
 
<embed
src=“xspf_player.swf?playlist_url=playlist.xspf”
quality=”high” bgcolor=”#E6E6E6″
name=”xspf_player” allowscriptaccess=”sameDomain”
type=”application/x-shockwave-flash”
pluginspage=”http://www.macromedia.com/go/getflashplayer”
align=”center” height=”168″ width=”400″></embed>
</object>
</body>
</html>

So when I click on the 'Create Playlist' button I get the following error:

Fatal error: Cannot instantiate non-existent class: recursivedirectoryiterator in 'playlist.php' line 18

What am I doing wrong?

Thank you and God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Duh, PHP5 is required.

OK I was using PHP4... thought I was using 5... OK so that part is corrected... now to continue working...

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Ok still have an issue...

Alrighty then...

Still having an issue but I think it has something to do with the RecursiveDirectoryIterator...

I get this error:

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/mp3/) [<a href='function.RecursiveDirectoryIterator---construct'>function.RecursiveDirectoryIterator---construct</a>]: failed to open dir: not implemented' in /home/content/site/html/sites/music/playlist.php:17 Stack trace: #0 /home/content/site/html/sites/music/playlist.php(17): RecursiveDirectoryIterator->__construct('/mp3/') #1 {main} thrown in /home/content/site/html/sites/music/playlist.php on line 17

This is what I am using:

<?php
 
//-----------------------------------------------------------------
//				Search for MP3s and their Album Art
//						*** requires PHP5 ***
//-----------------------------------------------------------------
 
// MP3 extension
$mp3 = ".mp3";
 
// Album Art Filename (Has to be the same in each folder that the MP3s are found.)
$albumart = AlbumArtSmall.jpg;
 
// Path to the root folder that you want to scan
$directory = "/mp3/";
 
$it = new RecursiveDirectoryIterator($directory);
 
foreach(new RecursiveIteratorIterator($it) as $file){
  if (!((strpos(strtolower($file), $mp3)) === false)){
    $items[] = preg_replace("#\\\#", "/", $file);
    $image = $directory.$albumart;
  }
}
sort($items);
 
// header("content-type:text/xml;charset=utf-8");
 
$filename=fopen('playlist.xspf',"w");
$contents = "<?xml version='1.0' encoding='UTF-8'?>\n
<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n
	<title>PHP Generated Playlist</title>\n
	<info>http://www.jeroenwijering.com/</info>\n\n
<trackList>\n
 
foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  $albumart = 
  $image = file_exists($albumart)
 
	<track>\n'
		<annotation>'.$title.'</annotation>\n
		<location>'.$item.'</location>\n
		<image>'.$image.'</image>\n
	</track>\n';
}
 
</trackList>\n
</playlist>\n";
 
fwrite($filename,utf8_encode($contents));
fclose($filename);
 
	echo "< script >window.open('player.htm')< /script >";
?>

Again the idea is this script would go through the folders on the server and gather the filename and other information to inject all of the MP3s into this XSPF file to allow an open source media player to dynamically play newer content without actually editing the playlist.

Thank you and God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Fixed that issue...

eh.. the problem was that I didn't add the '.' in the directory variable. In other words in order for it to work you can't have:
/mp3

Instead you should have:
./mp3

OK, now to continue working to create the XML file.

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
Isn't debugging fun

Isn't debugging fun!!!!

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 11/28/2008
Still can't get this working...

Hey everyone,

Hope you all had a blessed and safe weekend...

I still can't get this working right.

Here is my code:

<?php
 
//-----------------------------------------------------------------
//				Search for MP3s and their Album Art
//						*** requires PHP5 ***
//-----------------------------------------------------------------
 
// MP3 extension
$mp3 = ".mp3";
 
// Album Art Filename (Has to be the same in each folder that the MP3s are found.)
$albumart = "AlbumArtSmall.jpg";
 
// Path to the root folder that you want to scan
$directory = "mp3/";
 
/*** check if we have a valid directory ***/
if(!is_dir($directory)){
	//throw new Exception('Directory does not exist!'."\n");
	echo "<font color='red'>The $directory folder does not exist!</font><br />";
}
 
/*** check if we have permission to rename the files ***/
if( !is_readable( $directory )){
	//{throw new Exception('You do not have permissions to read this folder!'."\n");
	  echo "<font color='red'>You do not have permissions to read the $directory folder!</font><br />";
}
 
$it = new RecursiveDirectoryIterator(new RecursiveDirectoryIterator($directory));
 
foreach(new RecursiveIteratorIterator($it) as $file){
  if (!((strpos(strtolower($file), $mp3)) === false)){
    $items[] = preg_replace("#\\\#", "/", $file);
    $image = $directory.$albumart;
  }
}
sort($items);
 
// header("content-type:text/xml;charset=utf-8");
 
$filename=fopen('playlist.xspf',"w");
$header = "<?xml version='1.0' encoding='UTF-8'?>\n
<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n
	<title>PHP Generated Playlist</title>\n
	<info>http://www.jeroenwijering.com/</info>\n\n
<trackList>\n";
 
foreach($items as $item){
	$title_array = explode('/', $item);
	$title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
	$image = file_exists($albumart);
 
$tracks = "	<track>\n
		<annotation>$title</annotation>\n
		<location>$item</location>\n
		<image>$image</image>\n
	</track>\n";
}
 
$footer = "
</trackList>\n
</playlist>\n";
 
$contents = $header.$tracks.$footer;
 
fwrite($filename,utf8_encode($contents));
fclose($filename);
 
	echo "< script >window.open('player.htm')< /script >";
?>

The playlist.xspf does it created but it shows this:

<?xml version='1.0' encoding='UTF-8'?>
 
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
 
	<title>PHP Generated Playlist</title>
 
	<info>http://www.jeroenwijering.com/</info>
 
 
<trackList>
	<track>
 
		<annotation>the_unit</annotation>
 
		<location>./the_unit.mp3</location>
 
		<image></image>
 
	</track>
 
</trackList>
 
</playlist>

So what is creating that '.' in: ./the_unit.mp3

And why isn't it looking through the "mp3/" folders.

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Now I'm getting somewhere...

Hey everyone,

OK, I've gotten several things corrected.

To include my last post...

Anyway, what is happening now is that the list is created... but only the last file is added to the playlist.

Here is what I have thus far:

<?php
 
//-----------------------------------------------------------------
//				Search for MP3s and their Album Art
//						*** requires PHP5 ***
//-----------------------------------------------------------------
 
// MP3 extension
$mp3 = ".mp3";
 
// Album Art Filename (Has to be the same in each folder that the MP3s are found.)
$albumart = "AlbumArtSmall.jpg";
 
// Website URL
$site = "http://DOMAIN.com/";
 
// Path to the root folder that you want to scan
$directory = "mp3/";
 
// Check to see if we have a valid directory
if(!is_dir($directory)){
	echo "<font color='red'>The $directory folder does not exist!</font><br />";
}
 
// check to see if we have permission to read the files
if( !is_readable( $directory )){
	  echo "<font color='red'>You do not have permissions to read the $directory folder!</font><br />";
}
 
$it = new RecursiveDirectoryIterator($directory); // Was$it = new RecursiveDirectoryIterator(new RecursiveDirectoryIterator($directory));
 
foreach(new RecursiveIteratorIterator($it) as $file) {
  if (!((strpos(strtolower($file), $mp3)) === false)){
    $items[] = preg_replace("#\\\#", "/", $file);
    $image = $it.$albumart;
  }
  echo "$items";
}
// sort($items);
 
$filename=fopen('playlist.xspf',"w");
$header = "<?xml version='1.0' encoding='UTF-8'?>\n
<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n
	<title>PHP Generated Playlist</title>\n
	<info>http://www.jeroenwijering.com/</info>\n\n
<trackList>\n";
 
foreach($items as $item){
	$title_array = explode('/', $item);
	$title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
	$image = file_exists($albumart);
	$songlink = str_replace(" ","%20",$site.$item);
 
$tracks = "	<track>\n
		<annotation>$title</annotation>\n
		<location>$songlink</location>\n
		<image>$image</image>\n
	</track>\n";
}
 
$footer = "
</trackList>\n
</playlist>\n";
 
$contents = $header.$tracks.$footer;
 
fwrite($filename,utf8_encode($contents));
fclose($filename);
 
	echo "< script >window.open('player.htm')< /script >";
?>

The above outputs:

<?xml version='1.0' encoding='UTF-8'?>
 
<playlist version='1' xmlns='http://xspf.org/ns/0/'>
 
	<title>PHP Generated Playlist</title>
 
	<info>http://www.jeroenwijering.com/</info>
 
 
<trackList>
	<track>
 
		<annotation>14 - Paul Wilbur - Days of Elijah</annotation>
 
		<location>mp3/Paul%20Wilbur/Jerusalem%20Arise/14%20-%20Paul%20Wilbur%20-%20Days%20of%20Elijah.mp3</location>
 
		<image></image>
 
	</track>
 
</trackList>
 
</playlist>

So now it works!

Just need to figure out why it is only writing the last MP3 to the file...

God bless,
Johanthan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Almost done....

Hey hey,

OK, I've fixed the 'gathering of the album image' issue I was having...

Now, why doesn't it create more than one entry...

Current code:

<?php
 
//-----------------------------------------------------------------
//				Search for MP3s and their Album Art
//						*** requires PHP5 ***
//-----------------------------------------------------------------
 
// Album Image Filename (Change to that image file you use. Some use also 'AlbumArtSmall.jpg' as define by WMP.)
$imagename = "AlbumArtSmall.jpg";
 
// Website URL (Change to the URL to this file folder.)
$site = "http://DOMAIN.com/";
 
// Path to the root folder that you want to scan (The folder with your MP3s and other media.)
$directory = "mp3/";
 
// MP3/JPG extension -=Don't Change ANYTHING below this line!=-
$mp3 = ".mp3";
$jpg = ".jpg";
 
// Check to see if we have a valid directory
if(!is_dir($directory)){
	echo "<font color='red'>The $directory folder does not exist!</font><br />";
}
 
// check to see if we have permission to read the files
if( !is_readable( $directory )){
	  echo "<font color='red'>You do not have permissions to read the $directory folder!</font><br />";
}
 
// Gather the filenames of every file within the folder/subfolder of $directory
$files = new RecursiveDirectoryIterator($directory);
 
 
// For each file gather information
foreach(new RecursiveIteratorIterator($files) as $file) {
 
// Gather the MP3 files.
  if (!((strpos(strtolower($file), $mp3)) === false)){
    $mp3s[] = preg_replace("#\\\#", "/", $file);
  }
//	echo "$mp3s<br />";
 
// Gather the image file of each album
  if (!((strpos(strtolower($file), $jpg)) === false)){
    $jpgs = preg_replace("#\\\#", "/", $file);
//	echo "$jpgs<br />";
 
// Gather the folder only of each image file
	$artpath = $site.dirname($jpgs)."\\".$imagename; // Was $folder
//	echo "$artpath";
	$correct = "/";
	$albumart = str_replace("\\", $correct, $artpath);
//	echo "$albumart";
 
 
// Replace the last "\" with a "/"
//	$folderredo = str_replace("#\\\#", "/", $jpgs);
//	echo "$folderredo<br />";
//	$albumart = $site.$folder.$imagename;
//	echo "$albumart";
  }
}
 
// Sort the items found
sort($mp3s);
 
// Open the new playlist.xspf file to write to it
$filename=fopen('playlist.xspf',"w");
 
// Header of the XSPF file
$header = "<?xml version='1.0' encoding='UTF-8'?>\n
<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n
	<title>PHP Generated Playlist</title>\n
	<info>http://www.jeroenwijering.com/</info>\n\n
<trackList>\n";
 
// Gather each MP3 file and its information
foreach($mp3s as $mp3){
	$title_array = explode('/', $mp3);
	$title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
	$songlink = str_replace(" ","%20",$site.$mp3);
	$artlink = str_replace(" ","%20",$albumart);
 
	$tracks = "	<track>\n
			<annotation>$title</annotation>\n
			<location>$songlink</location>\n
			<image>$artlink</image>\n
		</track>\n";
}
 
 
// Footer of the XSPF file
$footer = "
</trackList>\n
</playlist>\n";
 
// Gather all contents from above
$contents = $header.$tracks.$footer;
 
// Write the $contents to plsylist.xspf
fwrite($filename,utf8_encode($contents));
 
// Close the playlist.xspf file
fclose($filename);
 
// Launch the player with the newly created playlist.xspf file (NOTE: this file is defined in the following HTML file.)
	echo "<script>window.open('player.htm')</script>";
?>

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Any ideas?

Any ideas?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Is the issue my 'foreach' construct?

Is the issue my 'foreach' construct?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Add a period

You're seeing the last track right? Your issue is:

$tracks = "<track>\n
<annotation>$title</annotation>\n
<location>$songlink</location>\n
<image>$artlink</image>\n
</track>\n";

and it should be

$tracks .= "<track>\n
<annotation>$title</annotation>\n
<location>$songlink</location>\n
<image>$artlink</image>\n
</track>\n";

Note the period before the equals sign. This way your string is appended to each time. That'll get you every time or at least it has me.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
Booyah!

@Bishop Booyah,

Awesome work... yeah that did it for the music... now I've got a 800+ auto playlist that created itself on the fly!

Awesome....

One problem though... the same image file is being used for each song... how can I correct this?

My next step is to move away from the filename as the title and use the tags on each MP3 instead... but I want to get this finished 1st...

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Make your albumart variable

Make your albumart variable an array like your mp3s array. Or make $mp3s array multidimensional ie $mp3s = array(song -> mp3info, albumart -> artinfo)

For tag info use getid3 (http://getid3.sourceforge.net/) if the id3 tag has albumart in it, you could use that instead of pointing to an external file. Might help out.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
I am already working on the

I am already working on the getID3 stuff... great minds think alike...

I did make the $jpgs an array, and when they are echoed out, they show up as arrays... just like the $mp3s works.

Yet it doesn't get the albumart right but does the MP3s right.

More code:

 
// Gather the MP3 files.
  if (!((strpos(strtolower($file), $mp3ext)) === false)){
    $mp3s[] = preg_replace("#\\\#", "/", $file);
  }
//	echo "$mp3s<br />";
 
// Gather the image file of each album
  if (!((strpos(strtolower($file), $jpgext)) === false)){
    $jpgs[] = preg_replace("#\\\#", "/", $file);
//	echo "$jpgs<br />";
 
	$artpath = str_replace("#\\\#", "/", $jpgs);
	$albumart = $site.$artpath;

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
You're sorting the mp3 array,

You're sorting the mp3 array, are you also sorting the jpg array?

If the indexes are the same (ie $mp3[1] = $jpg[1]) then simply use the index from mp3 to index the jpg.

foreach($mp3s as $index => $mp3){
$title_array = explode('/', $mp3);
$title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
$songlink = str_replace(" ","%20",$site.$mp3);
$artlink = str_replace(" ","%20",$albumart[$index]);
$tracks = " <track>\n
<annotation>$title</annotation>\n
<location>$songlink</location>\n
<image>$artlink</image>\n
</track>\n";
}

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
That was interesting....

Well, that was interesting... I did as you suggested... and it took each character of a link to an image and placed that single character in each of the variables:

<image>h</image>
<image>t</image>
<image>t</image>
<image>p</image>
<image>:</image>
<image>/</image>
<image>/</image>
<image>m</image>
<image>u</image>
<image>s</image>
<image>i</image>
<image>c/image>
<image>.</image>
<image>a</image>
<image>g</image>
...

Any other ideas?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Yeah, your $albumart needs to

Yeah, your $albumart needs to be an array or just use the $jpgs

Maybe:

  if (!((strpos(strtolower($file), $jpgext)) === false)){
    $jpgs[] = $site.str_replace("#\\\#", "/", preg_replace("#\\\#", "/", $file));
  }

...

foreach($mp3s as $index => $mp3){
  $title_array = explode('/', $mp3);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  $songlink = str_replace(" ","%20",$site.$mp3);
  $artlink = str_replace(" ","%20",$jpgs[$index]);
  $tracks = " <track>\n
  <annotation>$title</annotation>\n
  <location>$songlink</location>\n
  <image>$artlink</image>\n
  </track>\n";
}

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
missing .=

I noticed the missing . before the = seems to have popped up again in the snippets.

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 10/18/2008
I told you those dots are

I told you those dots are tricky!

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
OK so I am using the

OK so I am using the following:

// For each file gather information
foreach(new RecursiveIteratorIterator($files) as $file) {
 
// Gather the MP3 files.
  if (!((strpos(strtolower($file), $mp3ext)) === false)){
    $mp3s[] = preg_replace("#\\\#", "/", $file);
  }
//	echo "$mp3s<br />";
 
// Gather the image file of each album
  if (!((strpos(strtolower($file), $jpgext)) === false)){
    $jpgs[] = preg_replace("#\\\#", "/", $file);
//	echo "$jpgs<br />";
 
// Include the Album Art Configuration file
include("config/albumartconfig.php");
  }
}
 
// Sort the items found
sort($mp3s);
sort($jpgs);
 
foreach($mp3s as $index => $mp3){ // Was foreach($mp3s as $mp3){
 
	include("config/titleconfig.php");
 
	$songlink = str_replace(" ","%20",$site.$mp3);
	foreach($jpgs as $jpg){
		$artlink = str_replace(" ","%20",$site.$jpgs[$index]); // Was $artlink = str_replace(" ","%20",$albumart);
	}
 
	$tracks .= "	<track>\n
			<annotation>$title</annotation>\n
			<location>$songlink</location>\n
			<image>$artlink</image>\n
		</track>\n";
}

OK, the images are working... but because there are less images and more MP3, the 4 images available link to the 1st four MP3s, then the next 4 images, from the next album, link to the next four MP3s... in the previous album.

So how can we link the image in the same folder to each MP3 in that folder?

Can we not use what I have here?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Seriously, you'd save

Seriously, you'd save yourself a lot of problems just by getting the albumart via getid3.

You can make the $mp3s a multidimensional array(directory, mp3file => mp3, artwork => jpg)


foreach(new RecursiveDirectoryIterator($directory) as $dir){

  $path = RecursiveDirectoryIterator::key;

  // For each file gather information
  foreach(new RecursiveIteratorIterator($dir) as $file) {

    // Gather the MP3 files.
    if (!((strpos(strtolower($file), $mp3ext)) === false))
      $mp3s[] = preg_replace("#\\\#", "/", $file);

    // Gather the image file of each album
    if (!((strpos(strtolower($file), $jpgext)) === false))
      $artwork = preg_replace("#\\\#", "/", $file);
  }
  $albumfolders[$path][mp3s] = $mp3s;
  $albumfolders[$path][artwork] = $artwork;
}

// Sort the albums by directory
asort($albumfolders);

foreach($albumfolders as $album) {

  // sort the individual album mp3s
  asort($album[mp3s]);
  $artwork = $album[artwork];

  foreach ($album[mp3s] as $mp3){
    include("config/titleconfig.php");
    $songlink = str_replace(" ","%20",$site.$mp3);
    $artlink = str_replace(" ","%20",$site.$artwork);

    $tracks .= "<track>\n
    <annotation>$title</annotation>\n
    <location>$songlink</location>\n
    <image>$artlink</image>\n
    </track>\n";
  }
}

Try that, I doubt if it will work since I didn't run it. I'm just shooting in the dark but it should help give you a direction. Print out the variables as you go along and that'll help you traverse the array of arrays.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
GetID3() was my next step...

GetID3() was my next step... I'd rather use that...

The link to that would be:
http://www.tsgcomputers.net/getID3/getid3.php

Can you rewrite that with the link...

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Got this error when i ran

Got this error when i ran it:

Fatal error: Undefined class constant 'key'

I bet this has to deal with getID3()?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
getID3()

I did try to use the getID3... and I get this error:

Fatal error: Class 'getID3' not found at line 15

Here is line 15 in which it is complaining about:

$getID3 = new getID3;

Here is the whole code (I took out the bunch of comments I had.):

<?php
// Tried using an include however to be able to use this for any site, the following is needed:
//include("http://www.tsgcomputers.net/getid3/getid3.php");
$getid3php = file_get_contents("http://www.tsgcomputers.net/getid3/getid3.php");
 
echo $getid3php;
 
$getID3 = new getID3;
 
$analyzefiles = $getID3->analyze($files);
 
getid3_lib::CopyTagsToComments($analyzefiles);
 
$artist = @$analyzefiles['comments_html']['artist'][0]; // artist from any/all available tag formats
$song = @$analyzefiles['tags']['id3v2']['title'][0]; // title from ID3v2
 
	$title = $artist.$song;
?>

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Why not use the sourceforge

Why not use the sourceforge library? http://getid3.sourceforge.net

They've got a ton of examples.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
Guess I am still a bit

Guess I am still a bit confused about how it is supposed to work with my code... what do I have to put in my code for it to work?

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Just download the latest

Just download the latest stable package 1.7.7, unzip it into your working directory and then you can run one of the many demos included in the package.

One of the simple demos will scan a directory for you: http://getid3.sourceforge.net/source/demo.simple.phps

There are a lot of different demos so you should be able to find one that almost does what you want and then modify it. Getid3 is nice because you can even append audio to an mp3 clip. Pretty cool.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
I got it...

Hey everyone,

I actually figured it out...

Have you ever had a problem that has been plaquing you for days.. then you get a good night's rest and wake up with the solution?

Like an epiphany or a lightbulb moment?

Exactly what happened...

And it was so easy too...

OK, so I already had an array setup for the mp3s... and I was trying to use another array for the jpgs... that was my problem...

The solution... I used the mp3 array I already had and added that variable for the image to use in that same function and... voila!

Problem solved...

Now how to get getID3 to get the artist and song names...

@Booyah,

I did see a lot of examples... but the problem I found was that it wasn't explained how you can input those into your code to gather that info....

All I need is the artist and the song title...

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Just find an example that

Just find an example that does what you want and grab the body of the code (inside the head & body tags) and insert it into your script wherever it fits.

The only thing you'll really have to do is change the path to the id3 library. For example from the demo.basic.phps:

require_once('the/directory/where/you/unzipped/getid3/getid3.php);

// Initialize getID3 engine
$getID3 = new getID3;

// Inside your foreach where you print out each mp3 do this
foreach($mp3s as $index => $mp3){

// ... your code ...

// Analyze file and store returned data in $ThisFileInfo
$ThisFileInfo = $getID3->analyze($mp3);

// Output desired information in whatever format you want
echo @$ThisFileInfo['comments_html']['artist'][0]; // artist from any/all available tag formats
echo @$ThisFileInfo['tags']['id3v2']['title'][0];  // title from ID3v2
echo @$ThisFileInfo['audio']['bitrate'];           // audio bitrate
echo @$ThisFileInfo['playtime_string'];            // playtime in minutes:seconds, formatted string

// ... print out your track xml ...

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
I did add some of that in...

I did go ahead and worked a little on it and this is what I have:

include('/home/content/html/getid3/getid3.php');
 
$getID3 = new getID3;
 
$analyzefiles = $getID3->analyze($files);
 
getid3_lib::CopyTagsToComments($analyzefiles);
 
$artist = @$analyzefiles['comments_html']['artist'][0];
$song = @$analyzefiles['tags']['id3v2']['title'][0];
 
$title = $artist.$song;

I get this error when I try to execute that...

Fatal error: Cannot redeclare class getID3 in /home/content/html/getid3/getid3.php on line 19

Any ideas?

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Is either the include

Is either the include statement or the "new getID3" statement inside your loop? They need to be outside any loops.

Also, use "include_once" or "require_once" so that php doesn't try and include them a second time.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
OK it did finish the

OK it did finish the operating without errors... but now it shows everything undefined... will look into that and report back with what I find.

Thank you Booyah!

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
Whole file

Can you post the whole file you are working on. That might help.

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 11/28/2008
OK, here is the contents of

OK, here is the contents of the titleid3.php file:

<?php
//-----------------------------------------------------------------
//				Gather the ID3 tag information
//-----------------------------------------------------------------
 
include_once('/home/content/html/getid3/getid3.php');
 
$getID3 = new getID3;
 
$analyzefiles = $getID3->analyze($mp3);
 
getid3_lib::CopyTagsToComments($analyzefiles);
 
$artist = @$analyzefiles['comments_html']['artist'][0]; // artist from any/all available tag formats
$song = @$analyzefiles['tags']['id3v2']['title'][0]; // title from ID3v2
 
$title = $artist.$song;
?>

The path is correct.

Here is the section that includes the above file in the Playlist.php file:

// Gather each MP3 file and its information
foreach($mp3s as $mp3){
 
	// Include the Album Art Configuration file
	include("config/albumartconfig.php");
	// Include the Song Title Configuration file
	include("config/titleconfig.php");
 
	$songlink = str_replace(" ","%20",$site.$mp3);
	$tracks .= "	<track>\n
			<annotation>$title</annotation>\n
			<location>$songlink</location>\n
			<image>$artlink</image>\n
		</track>\n";
}

The albumart is working correctly. it is just the title I can't seem to get.

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
Remove include_once('/home/co

Remove

include_once('/home/content/html/getid3/getid3.php');

$getID3 = new getID3;

From your titleid3 script and put them outside of the loop. You only need to declare the class once.

You can also put a

print "<pre>";
print_r($analyzefiles);
print "</pre>";

To see what getid3 is reading. But I would set your loop to break after one file so you don't get a screen full of stuff. Just insert "break;" at the end of your loop.

Are you sure the id3 tags have information in them?

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
OK I understand

OK I understand now....

Although I've been told about this library before only needing to be loaded once... I hadn't thought about removing it from the loop.

The ID tags are loading correctly.... some of the songs aren't right but that is another matter for me to fix.

Thank you so much... it is working now.

Sorry for all the trouble... the getID3() looks more confusing than it really is. And finding the right information to use it correctly took some looking into... figured it would be easier than going through a forum...

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 11/28/2008
Sort() by date

Hey everyone,

Can you sort the sort() by date? If you can how... if not how?

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to:
Joined: 10/18/2008
a) Why would you need this?

a) Why would you need this? and b) How important is this?

It can be done, but is this for a sermon series or just a music playlist or what?

This is more complicated, but you would basically need a two dimensional array (file=> $file, date => $filemodifieddate), then sort it on the date.

If this is for a sermon series, then I'd just suggest using the date as part of the file name since it's easier to sort on the file name than the date.

But if it's important you should probably post your code and I'll try to help you out.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 11/28/2008
Question...

Can the 'RecursiveDirectoryIterator' be used with a URL as opposed to a path?

Is there a way to make it work with a URL?

God bless,
Johnathan

Do you want to learn more about the Christian Worldview or need some prayer or other support? Go to: