Generate playlist using php and getid3
New to php.
I use a php script to generate a playlist, the first song entry in the list is blank for all fields (artist, title, length, filename). Why?
Her开发者_如何学Pythone's the script
<?php
require_once('getid3/getid3.php');
$dir = 'mp3';
$file_type = 'mp3';
$play_list = '<?xml version="1.0" encoding="utf-8"?><config>';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
$name_array = explode('.', $file);
if ($name_array[1] == $file_type) {
$play_list .= '<song>';
$file = "$dir/$file";
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($file);
getid3_lib::CopyTagsToComments($ThisFileInfo);
$play_list .= '<artist>'.$ThisFileInfo['comments_html']['artist'][0] . '</artist>';
$play_list .= '<title>'. $ThisFileInfo['tags']['id3v2']['title'][0]. '</title>';
$new_playtime = ereg_replace("[^A-Za-z0-9]", "", $ThisFileInfo['playtime_string'] );
$play_list .= '<length>'.$new_playtime . '</length>';
$play_list .= '<fileName>'.$file.'</fileName>';
$play_list .= '</song>';
}
}
}
closedir($dh);
$play_list .= '</config>';
echo "$play_list";
}
}
?>
And here's how the ouput looks.
<?xml version="1.0" encoding="utf-8"?>
<config>
<song><artist></artist><title></title><length></length><fileName>mp3/air - all i need.mp3</fileName></song>
<song><artist>Air</artist><title>Remember</title><length>234</length><fileName>mp3/air - remember.mp3</fileName></song>
<song><artist>Air</artist><title>You Make It Easy</title><length>401</length><fileName>mp3/air - you make it easy.mp3</fileName></song>
<song><artist>Ian Brown</artist><title>Dolphins Were Monkeys (Single Version)</title><length>258</length><fileName>mp3/Ian Brown - Dolphins Were Monkeys.mp3</fileName></song>
<song><artist>Ian Brown</artist><title>F.E.A.R.</title><length>431</length><fileName>mp3/Ian Brown - Fear.mp3</fileName></song>
<song><artist>Ian Brown</artist><title>Keep What Ya Got</title><length>348</length><fileName>mp3/Ian Brown - Keep What Ya Got.mp3</fileName></song>
<song><artist>Ian Brown</artist><title>My Star</title><length>509</length><fileName>mp3/Ian Brown - My Star.mp3</fileName></song>
<song><artist>Ian Brown</artist><title>Time Is My Everything</title><length>354</length><fileName>mp3/Ian Brown - Solarized - 02 - Time Is My Everything.mp3</fileName></song></config>
The filename field isn't empty :
<song><artist></artist><title></title><length></length><fileName>mp3/air - all i need.mp3</fileName></song>
Maybe it's just because the ID3 tags aren't set for "all i need.mp3" ?
P.S : you should use pathinfo
with PATHINFO_EXTENSION
instead of an explode
, because if a song contains several dots it won't be matched.
Try using glob() to get the files. http://us.php.net/manual/en/function.glob.php
$files = glob($dir . '/*.mp3');
foreach ($files as $file) {
...
}
Use pathinfo() to get the file extension information. http://php.net/manual/en/function.pathinfo.php
Although, if you use the glob() function above, you wouldn't have to, since the *.mp3 will already filter the mp3 files.
精彩评论