Skip to content


[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.

(I apologize for not fine tuning this code with preloaders and creating other classes, I was running out of spare time at the moment)

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

package
{
	import flash.display.Sprite;
	public class Rectangle extends Sprite
	{
		private var xPos:Number;
		private var yPos:Number;
		private var rWidth:Number;
		private var rHeight:Number;
		private var color:uint;

		public function Rectangle(xPos:Number=0,yPos:Number=0,rWidth:Number=10,rHeight:Number=10,color:uint=0x336699)
		{
			this.graphics.beginFill(color);
			this.graphics.drawRect(xPos,yPos,rWidth,rHeight);
			this.graphics.endFill();
		}
	}
}

Here is the document class

package
{
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.media.*
	import flash.net.URLRequest;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.*;

	public class LoadSound extends Sprite
	{
		private var snd:Sound;
		private var sndChannel:SoundChannel;
		private var output:TextField;
		private var id3Array:Array;
		private var ct:uint;

		private var image1:Loader = new Loader();
		private var image2:Loader = new Loader();

		private var mask1:Rectangle;
		private var mask2:Rectangle;

		public function LoadSound()
		{
			showInstructions();
			showGraphics();
			init();
		}

		private function init():void
		{
			var snd:Sound = new Sound();
			snd.load(new URLRequest("/path/to/mp3/file/"));

			snd.addEventListener(IOErrorEvent.IO_ERROR, sndIOError);
			snd.addEventListener(ProgressEvent.PROGRESS, sndProgress);
			snd.addEventListener(Event.COMPLETE, sndComplete);
			snd.addEventListener(Event.ID3, ID3content);

		}

		private function sndIOError(e:IOErrorEvent):void
		{
			output.text = "An error occured when laoding the sound";
		}

		private function sndProgress(e:ProgressEvent):void
		{
			output.text = "Loading: " + Math.round(100 * e.bytesLoaded / e.bytesTotal);
		}

		private function sndComplete(e:Event):void
		{
			output.text = "Sound loaded - click the stage to play and stop";

			stage.addEventListener(MouseEvent.MOUSE_DOWN, toggleMusic);

			var music:Sound = e.target as Sound;

			function toggleMusic():void
			{
				ct++;

				if (ct%2)
				{
					sndChannel = music.play();
					addEventListener(Event.ENTER_FRAME, playSpectrum);
				}
				else
				{
					sndChannel.stop();
					removeEventListener(Event.ENTER_FRAME, playSpectrum);
				}
			}
		}

		private function ID3content(e:Event):void
		{
			var id3Prop:ID3Info = e.target.id3;

			var id3Array:Array = new Array();

			for (var idName:String in id3Prop)
			{
				//trace ("ID3 Tag",idName, "=", id3Prop [idName]);
				id3Array.push(new Array(idName, id3Prop [idName]));
			}

			id3Text.text = "";

			for (var h:Number=0; h < id3Array.length; h++)
			{
				// id3Text is the name of the TextArea component I placed on the stage in the .fla movie
				// I also set the parameters for the componenet in the .fla movie
				id3Text.text += id3Array[h][0] + " = " + id3Array[h][1] + "\n";
			}
		}

		private function playSpectrum(e:Event):void
		{
			mask1.y = sndChannel.leftPeak * -100;
			mask2.y = sndChannel.rightPeak * -100;
		}

		private function showInstructions():void
		{
			// Small Black Text Formatting
			var insText:TextFormat = new TextFormat();
			insText.font = "Arial";
			insText.color = 0xffffff;
			insText.size = 12;
			insText.underline = false;

			// output TextField
			output = new TextField();
			output.defaultTextFormat = insText;
			addChild(output);
			output.x = 10;
			output.y = 35;
			output.width = stage.stageWidth;
			output.height = 22;
		}

		private function showGraphics():void
		{
			image1.load(new URLRequest("http://manewc.com/projects/flash/LoadSoundID3/gradient.jpg"));
			addChild(image1);
			image1.x = 10;
			image1.y = 200;

			image2.load(new URLRequest("http://manewc.com/projects/flash/LoadSoundID3/gradient2.jpg"));
			addChild(image2);
			image2.x = stage.stageWidth - 244;
			image2.y = 200;

			// create an object to act as our mask
			mask1 = new Rectangle(image1.x, image1.y + 99, 100, 100);
			addChild(mask1);

			mask2 = new Rectangle(image2.x, image2.y + 99, 100, 100);
			addChild(mask2);

			// set the masks:
			image1.mask = mask1;
			image2.mask = mask2;
		}
	}
}

Posted in Actionscript 3, Audio.


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. [AS 3] Loading External .mp3 File and Retrieving ID3 Information « Flash ActionScript(AS) Expert linked to this post on May 1, 2009

    [...] I needed a mask for the analyzer, so I used my Rectangle.as class. view plaincopy to [...]



Some HTML is OK

or, reply to this post via trackback.