ProgressBarTest_01.as

package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.ID3Info;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	
	public class ProgressBarTest_01 extends Sprite {
		
		private var sound:Sound;
		private var channel:SoundChannel;
		
		private var artist:String;
		private var songName:String;
		
		private var displayLabel:Label;
		private var loadButton:Button;
		private var progressBar:ProgressBar;
		
		private static var soundURL:String = "http://www.helpexamples.com/flash/sound/song1.mp3";
		
		public function ProgressBarTest_01() {
			init();
		}
		
		private function init():void {
			stage.align = StageAlign.TOP_LEFT;
			stage.showDefaultContextMenu = false;
			
			assignComponentsReference();
			configureComponents();
		}
		
		private function assignComponentsReference():void {
			displayLabel = mcLabel;
			loadButton = mcButton;
			progressBar = mcProgressBar;
		}
		
		private function configureComponents():void {
			progressBar.mode = ProgressBarMode.EVENT;
			loadButton.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
		}
		
		private function onMouseClick(event:MouseEvent):void {
			loadButton.enabled = false;
			loadSound();
		}
		
		private function loadSound():void {
			sound = new Sound();
			sound.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent, false, 0, true);
			sound.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 0, true);
			sound.addEventListener(Event.ID3, onID3Event, false, 0, true);
			
			progressBar.source = sound;
			progressBar.addEventListener(ProgressEvent.PROGRESS, onLoadProgress, false, 0, true);
			progressBar.addEventListener(Event.COMPLETE, onLoadComplete, false, 0, true);
			try {
				var context:SoundLoaderContext = new SoundLoaderContext();
				context.checkPolicyFile = true;
				sound.load(new URLRequest(soundURL + "?uniqueNumber=" + new Date().getTime()), context);
			} catch (err:Error) {
				trace(err.message);
			}
		}
		
		private function removeListeners():void {
			sound.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent);
			sound.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			sound.removeEventListener(Event.ID3, onID3Event);
			
			progressBar.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress);
			progressBar.removeEventListener(Event.COMPLETE, onLoadComplete);
		}
		
		private function onIOErrorEvent(event:IOErrorEvent):void {
			displayLabel.text = "IOErrorEvent : URL NotFound";
			removeListeners();
		}
		
		private function onSecurityError(event:SecurityErrorEvent):void {
			displayLabel.text = "SecurityError : Sandbox Violation";
			removeListeners();
		}
		
		private function onLoadProgress(event:ProgressEvent):void {
			displayLabel.text = "sound loading... " + Math.round(progressBar.percentComplete) + "%";
		}
		
		private function onLoadComplete(event:Event):void {
			channel = sound.play();
			channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete, false, 0, true);
			displayLabel.text = songName + ".mp3 - " + artist;
			removeListeners();
			trace("File size: " + sound.bytesTotal + "bytes");
		}
		
		private function onID3Event(event:Event):void {
			var id3:ID3Info = sound.id3;
			for (var item:String in id3) {
				trace(item, " : ", id3[item]);
			}
			
			artist = id3.artist;
			songName = id3.songName;
			
			if (artist == null) {
				artist = "Unknown artist";
			}
			
			if (songName == null) {
				songName = "There is no data";
			}
		}
		
		private function onSoundComplete(event:Event):void {
			channel.stop();
			channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			
			progressBar.reset();
			progressBar.source = null;
			loadButton.enabled = true;;
			displayLabel.text = "Play again?";
		}
	}
}


ProgressBarTest_02.as

package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.ID3Info;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	
	public class ProgressBarTest_02 extends Sprite {
		
		private var sound:Sound;
		private var channel:SoundChannel;
		
		private var artist:String;
		private var songName:String;
		
		private var displayLabel:Label;
		private var loadButton:Button;
		private var progressBar:ProgressBar;
		
		private static var soundURL:String = "http://www.helpexamples.com/flash/sound/song1.mp3";
		

		/* 상략... */


		private function configureComponents():void {
			progressBar.mode = ProgressBarMode.POLLED;
			loadButton.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
		}

		/* 중략... */

		private function onLoadProgress(event:ProgressEvent):void {
			var percent:uint = Math.round(sound.bytesLoaded / sound.bytesTotal * 100);
			displayLabel.text = "sound loading... " + percent + "%";
			trace(Math.round(sound.bytesLoaded / sound.bytesTotal * 100) + "%");
		}


		/* 하략... */


	}
}