Flash ActionScript(AS) Expert

[AS 3] Loading External .mp3 File and Retrieving ID3 Information

Posted by: sarapeter on: May 1, 2009

[AS 3] Loading External .mp3 File and Retrieving ID3 Information

I just discovered that Actionscript 3 has the ability to read the data that some .mp3 files have in them. To demonstrate I created a little movie below that will output all this data and dump it in a TextArea Component. In addition, I created a little analyzer demo for the right and left channels.

First I needed a mask for the analyzer, so I used my Rectangle.as class.

  1. package   
  2. {   
  3.     import flash.display.Sprite;   
  4.     public class Rectangle extends Sprite   
  5.     {   
  6.         private var xPos:Number;   
  7.         private var yPos:Number;   
  8.         private var rWidth:Number;   
  9.         private var rHeight:Number;   
  10.         private var color:uint;   
  11.   
  12.         public function Rectangle(xPos:Number=0,yPos:Number=0,rWidth:Number=10,rHeight:Number=10,color:uint=0×336699)   
  13.         {   
  14.             this.graphics.beginFill(color);   
  15.             this.graphics.drawRect(xPos,yPos,rWidth,rHeight);   
  16.             this.graphics.endFill();   
  17.         }   
  18.     }   
  19. }  

Here is the document class

  1. package   
  2. {   
  3.     import flash.display.Sprite;   
  4.     import flash.display.Loader;   
  5.     import flash.media.*   
  6.     import flash.net.URLRequest;   
  7.     import flash.events.IOErrorEvent;   
  8.     import flash.events.ProgressEvent;   
  9.     import flash.events.Event;   
  10.     import flash.events.MouseEvent;   
  11.     import flash.text.*;   
  12.   
  13.     public class LoadSound extends Sprite   
  14.     {   
  15.         private var snd:Sound;   
  16.         private var sndChannel:SoundChannel;   
  17.         private var output:TextField;   
  18.         private var id3Array:Array;   
  19.         private var ct:uint;   
  20.   
  21.         private var image1:Loader = new Loader();   
  22.         private var image2:Loader = new Loader();   
  23.   
  24.         private var mask1:Rectangle;   
  25.         private var mask2:Rectangle;   
  26.   
  27.         public function LoadSound()   
  28.         {   
  29.             showInstructions();   
  30.             showGraphics();   
  31.             init();   
  32.         }   
  33.   
  34.         private function init():void  
  35.         {   
  36.             var snd:Sound = new Sound();   
  37.             snd.load(new URLRequest(“/path/to/mp3/file/”));   
  38.   
  39.             snd.addEventListener(IOErrorEvent.IO_ERROR, sndIOError);   
  40.             snd.addEventListener(ProgressEvent.PROGRESS, sndProgress);   
  41.             snd.addEventListener(Event.COMPLETE, sndComplete);   
  42.             snd.addEventListener(Event.ID3, ID3content);   
  43.   
  44.         }   
  45.   
  46.         private function sndIOError(e:IOErrorEvent):void  
  47.         {   
  48.             output.text = “An error occured when laoding the sound”;   
  49.         }   
  50.   
  51.         private function sndProgress(e:ProgressEvent):void  
  52.         {   
  53.             output.text = “Loading: ” + Math.round(100 * e.bytesLoaded / e.bytesTotal);   
  54.         }   
  55.   
  56.         private function sndComplete(e:Event):void  
  57.         {   
  58.             output.text = “Sound loaded - click the stage to play and stop”;   
  59.   
  60.             stage.addEventListener(MouseEvent.MOUSE_DOWN, toggleMusic);   
  61.   
  62.             var music:Sound = e.target as Sound;   
  63.   
  64.             function toggleMusic():void  
  65.             {   
  66.                 ct++;   
  67.   
  68.                 if (ct%2)   
  69.                 {   
  70.                     sndChannel = music.play();   
  71.                     addEventListener(Event.ENTER_FRAME, playSpectrum);   
  72.                 }   
  73.                 else  
  74.                 {   
  75.                     sndChannel.stop();   
  76.                     removeEventListener(Event.ENTER_FRAME, playSpectrum);   
  77.                 }   
  78.             }   
  79.         }   
  80.   
  81.         private function ID3content(e:Event):void  
  82.         {   
  83.             var id3Prop:ID3Info = e.target.id3;   
  84.   
  85.             var id3Array:Array = new Array();   
  86.   
  87.             for (var idName:String in id3Prop)   
  88.             {   
  89.                 //trace (“ID3 Tag”,idName, ”=”, id3Prop [idName]);   
  90.                 id3Array.push(new Array(idName, id3Prop [idName]));   
  91.             }   
  92.   
  93.             id3Text.text = “”;   
  94.   
  95.             for (var h:Number=0; h < id3Array.length; h++)   
  96.             {   
  97.                 // id3Text is the name of the TextArea component I placed on the stage in the .fla movie   
  98.                 // I also set the parameters for the componenet in the .fla movie   
  99.                 id3Text.text += id3Array[h][0] + “ = ” + id3Array[h][1] + “\n”;   
  100.             }   
  101.         }   
  102.   
  103.         private function playSpectrum(e:Event):void  
  104.         {   
  105.             mask1.y = sndChannel.leftPeak * -100;   
  106.             mask2.y = sndChannel.rightPeak * -100;   
  107.         }   
  108.   
  109.         private function showInstructions():void  
  110.         {   
  111.             // Small Black Text Formatting   
  112.             var insText:TextFormat = new TextFormat();   
  113.             insText.font = “Arial”;   
  114.             insText.color = 0xffffff;   
  115.             insText.size = 12;   
  116.             insText.underline = false;   
  117.   
  118.             // output TextField   
  119.             output = new TextField();   
  120.             output.defaultTextFormat = insText;   
  121.             addChild(output);   
  122.             output.x = 10;   
  123.             output.y = 35;   
  124.             output.width = stage.stageWidth;   
  125.             output.height = 22;   
  126.         }   
  127.   
  128.         private function showGraphics():void  
  129.         {   
  130.             image1.load(new URLRequest(“http://manewc.com/projects/flash/LoadSoundID3/gradient.jpg”));   
  131.             addChild(image1);   
  132.             image1.x = 10;   
  133.             image1.y = 200;   
  134.   
  135.             image2.load(new URLRequest(“http://manewc.com/projects/flash/LoadSoundID3/gradient2.jpg”));   
  136.             addChild(image2);   
  137.             image2.x = stage.stageWidth - 244;   
  138.             image2.y = 200;   
  139.   
  140.             // create an object to act as our mask   
  141.             mask1 = new Rectangle(image1.x, image1.y + 99, 100, 100);   
  142.             addChild(mask1);   
  143.   
  144.             mask2 = new Rectangle(image2.x, image2.y + 99, 100, 100);   
  145.             addChild(mask2);   
  146.   
  147.             // set the masks:   
  148.             image1.mask = mask1;   
  149.             image2.mask = mask2;   
  150.         }   
  151.     }   

Leave a Reply