我已成功编写AS3代码来编译XML列表。
但我想添加.mp3的真实比特率和专辑艺术。
我已经在PHP上成功完成了这项工作但是在Flash AS3上失败了。
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.media.ID3Info;
import flash.media.Sound;
var dir:String = "SOUNDS/";
var i:int = 1;
var songAmount:Number;
var soundTime:String;
var soundSize:String;
var soundBitrate:String;
var snd:Sound;
var snd_artist:Array = new Array();
var snd_album:Array = new Array();
var snd_title:Array = new Array();
var snd_time:Array = new Array();
var snd_size:Array = new Array();
var snd_bitrate:Array = new Array();
var snd_genre:Array = new Array();
var snd_year:Array = new Array();
var snd_comment:Array = new Array();
/*
var artLoader:URLLoader = new URLLoader();
artLoader.load(new URLRequest("id3albumart.php?folder="+dir));
*/
getSounds(i);
function getSounds(arg:int):void
{
snd = new Sound();
snd.load (new URLRequest(dir + String(arg) + ".mp3"));
snd.addEventListener(Event.COMPLETE, id3Loaded);
snd.addEventListener(IOErrorEvent.IO_ERROR, soundError,false,0,true);
}
function soundError(e:IOErrorEvent):void
{
songAmount = i-1;
doXML();
}
function id3Loaded(e:Event):void
{
/*** ARTIST ***/
if (snd.id3.artist == ""){
snd_artist.push("n/a");
} else {
snd_artist.push(snd.id3.artist);
}
/*** ALBUM ***/
if (snd.id3.album == ""){
snd_album.push("n/a");
} else {
snd_album.push(snd.id3.album);
}
/*** TITLE ***/
if (snd.id3.songName == ""){
snd_title.push("n/a");
} else {
snd_title.push(snd.id3.songName);
}
/*** DURATION ***/
var seconds:Number = snd.length / 1000;
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60)
seconds = Math.floor(seconds % 60);
soundTime = (hours < 10 ? "0" : "") + String(hours) + ":" + (minutes < 10 ? "0" : "") + String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if (soundTime == ""){
snd_time.push("n/a");
} else {
snd_time.push(soundTime);
}
/*** SIZE ***/
soundSize = String((snd.bytesTotal/1048576).toFixed(2)) + " MB";
snd_size.push(soundSize);
/*** BITRATE ***/
soundBitrate = 128 + " KBPS";
snd_bitrate.push(soundBitrate);
/*** YEAR ***/
if (snd.id3.year == ""){
snd_year.push("n/a");
} else {
snd_year.push(snd.id3.year);
}
/*** GENRE ***/
if (snd.id3.genre == ""){
snd_genre.push("n/a");
} else {
snd_genre.push(snd.id3.genre);
}
/*** COMMENT ***/
if (snd.id3.comment == ""){
snd_comment.push("n/a");
} else {
snd_comment.push(snd.id3.comment);
}
trackTEXT.text = "Tracks loaded: ";
numTEXT.text = i + "";
snd.close();
i++;
getSounds(i);
}
function doXML():void
{
var xml:XML = <songs/>;
for (i = 0; i < songAmount; i++)
{
var nodeName:XML = <song/>;
nodeName.appendChild(<number>{(i + 1)}</number>);
nodeName.appendChild(<artist>{snd_artist[i]}</artist>);
nodeName.appendChild(<album>{snd_album[i]}</album>);
nodeName.appendChild(<title>{snd_title[i]}</title>);
nodeName.appendChild(<time>{snd_time[i]}</time>);
nodeName.appendChild(<size>{snd_size[i]}</size>);
nodeName.appendChild(<bitrate>{snd_bitrate[i]}</bitrate>);
nodeName.appendChild(<genre>{snd_genre[i]}</genre>);
nodeName.appendChild(<year>{snd_year[i]}</year>);
nodeName.appendChild(<comment>{snd_comment[i]}</comment>);
xml.appendChild(nodeName);
}
var newXML:String = xml.toXMLString();
newXML = newXML.replace(/\n/g, "\r\n");
newXML = "<?xml version='1.0' encoding='utf-8'?>\r\n" + newXML;
var saveXML:FileReference = new FileReference();
saveXML.save(newXML, "mp3_XML.xml");
}