as 3加载mp3

package {
	import flash.display.*;
	import flash.events.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.ID3Info;
	import flash.net.URLRequest;
	import flash.external.ExternalInterface;
	import flash.utils.*;
	
	public class alarmSound extends Sprite{
		private var alarm:Sound;
		private var mp3URL:String = "http://img.3bu.cn/ring/ring/201003041507952.mp3"; //"alarm_2.mp3";
		private var song:SoundChannel;
		
		function alarmSound() {
			inited();
		}
		
		private function inited():void {
			alarm = new Sound();
			
			var req:URLRequest = new URLRequest(mp3URL);
			var buffer:SoundLoaderContext = new SoundLoaderContext(5*1000);
			
			alarm.addEventListener(Event.COMPLETE, completeHandler);
			alarm.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			alarm.addEventListener(ProgressEvent.PROGRESS, processHandler);
			alarm.addEventListener(Event.ID3, id3Handler);
			
			alarm.load(req, buffer);
			
			song = alarm.play(0, 0);//从0开始,循环1次
			
			song.addEventListener(Event.SOUND_COMPLETE, playCompleteHandler);			
			
		}
		
		private function processHandler(pro:ProgressEvent):void {
			var percent:Number = Math.floor(pro.bytesLoaded/pro.bytesTotal*100*100)/100;
			trace(pro.bytesLoaded + "---" + pro.bytesTotal + "已加载..." + percent + "%");
		}
		
		private function playCompleteHandler(...args):void {
			//trace(alarm.length/1000 + "---" + song.position/1000);
			
			var estimatedTotal:Number = Math.ceil(alarm.length / (alarm.bytesLoaded / alarm.bytesTotal));
			var position:Number = Math.round(100 * (song.position / estimatedTotal));
			
			trace(estimatedTotal/1000/60 + "----" + position);
		}
		
		private function completeHandler(evt:Event):void {
			alarm.removeEventListener(Event.COMPLETE, completeHandler);
			
			//trace(alarm.length + "---" + song.position);
		}
		
		private function ioErrorHandler(evt:Event):void {
			
		}
		
		private function id3Handler(evt:Event):void {
			var id3:ID3Info = alarm.id3;
			
			trace('音乐名称:' + id3.songName);
			trace('专辑: ' + id3.album);
			trace('艺术家:' + id3.artist);
		}
		
	}	
}
posted @ 2010-09-08 13:47  meteoric_cry  阅读(271)  评论(0编辑  收藏  举报