zoukankan      html  css  js  c++  java
  • 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);
    		}
    		
    	}	
    }
  • 相关阅读:
    BFS visit tree
    Kth Largest Element in an Array 解答
    Merge k Sorted Lists 解答
    Median of Two Sorted Arrays 解答
    Maximal Square 解答
    Best Time to Buy and Sell Stock III 解答
    Best Time to Buy and Sell Stock II 解答
    Best Time to Buy and Sell Stock 解答
    Triangle 解答
    Unique Binary Search Trees II 解答
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/1821370.html
Copyright © 2011-2022 走看看