0

I have successfully written AS3 code to compile an XML List.

But I want to add the true Bitrate of the .mp3's and the Album Art.

I have successfully done this on PHP but have failed miserably with 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");
}
helloflash
  • 2,457
  • 2
  • 15
  • 19
scottiescotsman
  • 73
  • 2
  • 12
  • Your code works fine. What kind of problem you got ? Did you got some errors ? Did you have mp3 files named 1.mp3, 2.mp3, .. in your sounds dir ? – akmozo Jan 01 '15 at 21:00
  • No errors but rather than reading the mp3 header for the bitrate & other tags [which I have no clue how to do] I just typed all mp3 are set to 128 kbps :( and need to extract the album art from the mp3 also ... I have read a few posts on some servers but cannot get it to save the picture to folder, but can on php. Thanks – scottiescotsman Jan 01 '15 at 23:27
  • OK, so your problem is to extract some tags like bitrate and album art, I thought It was saving data to an xml file. So, for bitrate take a look [here](http://stackoverflow.com/questions/6018224/getting-the-bit-rate-of-a-externally-loaded-mp3-in-the-flashplayer) and for album art, [here](http://stackoverflow.com/questions/11718051/album-art-of-music-file). – akmozo Jan 02 '15 at 00:27
  • I have used this code before and have never been able to get it to work on my project for the album art. Just tried to add it to the above code but failed miserably :( .... as all want to do is extract the .mp3 tags & save the album art @ same time rather than having to run 2 codes. – scottiescotsman Jan 02 '15 at 02:25
  • Ok I got the code to work, but it only shows 1 art picture out of 180 all others get ...Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type. Also its the same programme that tags the art to the mp3s.....help please – scottiescotsman Jan 03 '15 at 02:31

0 Answers0