AmbiencePlayer | 环境播放器 开发(二)

经过努力,FLEX端的编程已经完成了很大部分。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();" >
	<mx:Script>
		<![CDATA[
			import flash.media.Sound;
			import flash.media.SoundLoaderContext;
			import flash.net.URLRequest;
			import mx.rpc.events.ResultEvent;
			public var s:Sound = new Sound();
			public var channel:SoundChannel;
			public var t:Timer;
			public var lastPos:int = -1;
			public var lrc:Array = new Array();
			public function init():void{
				var req:URLRequest = new URLRequest(Application.application.parameters.url);
				var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
				s.load(req,context);
				fetchLRC.url = Application.application.parameters.lrc;
				fetchLRC.send();
			}
			public function lrcLoaded(event:ResultEvent):void{
				var lyrics:String = event.result.toString();
				lyrics=lyrics.replace("\r","");
				var tmp:Array = lyrics.split("\n");
				for(var i:int=0;i<tmp.length;i++){
					if(tmp[i].toString().charAt(0)=="["){
						var pattern:RegExp = /\[\d+:\d+\.\d+\]/g;
						var tmpArr:Array = String(tmp[i]).match(pattern);
						for(var j:int=0;j<tmpArr.length;j++){
							var p:RegExp = /[\[\]]/g;
							var tmp2:Array = String(tmpArr[j]).replace(p,"").split(":");
							lrc[String(int(Number(tmp2[0])*6000+Number(tmp2[1])*100))]=tmp[i].toString().replace(/^\[.+\]/g,"");
						}
					}
				}
				t=new Timer(10);
				t.addEventListener(TimerEvent.TIMER,executeLRC);
				channel = s.play();
				t.start();
				if(Application.application.parameters.loop=="true"){
					channel.addEventListener(Event.SOUND_COMPLETE,loopSound);
					function loopSound(event:Event):void{
						channel.stop();
						channel.removeEventListener(Event.SOUND_COMPLETE,loopSound);
						channel=null;
						t.reset();
						t.start();
						channel = s.play();
						channel.addEventListener(Event.SOUND_COMPLETE,loopSound);
					};
				}
			}
			public function executeLRC(event:TimerEvent):void{
				var position:int = (channel.position / 10);
				ExternalInterface.call("updatePosition",position);
				if(position!=lastPos){
					for(var i:int=lastPos;i<=position;i++){
					if(lrc[String(i)]!=null)
						if(String(lrc[String(i)]).charAt(0)=="{"){
							lrc[String(i)] = String(lrc[String(i)]).replace(String.fromCharCode(13),"");
							var preg:RegExp=/^\{.+:.+:.+:.+:.+:.+\}/g;
							var matArr:Array=String(lrc[String(i)]).match(preg);
							if(matArr.length==0){
								ExternalInterface.call("showLyrics",lrc[String(i)]);
							}else{
								//EXECUTE
								preg = /[{}]/g;
								var exec:Array = String(matArr[0]).replace(preg,"").split(":");
								trace(exec);
								if(String(exec[0]).charAt(0)=="#"){
									var HTMLElementId:String=String(exec[0]).substr(1,String(exec[0]).length);
									switch(String(exec[1]).toLowerCase()){
										case 'transition':{
											var p:RegExp = /^#/g;
											exec[3] = String(exec[3]).replace(p,"");
											exec[4] = String(exec[4]).replace(p,"");
											ExternalInterface.call("colorFade",HTMLElementId,String(exec[2]),exec[3],exec[4],20,int(Number(exec[5])));
										}break;
										case 'style':{
											ExternalInterface.call("eval","document.getElementById(\"" + HTMLElementId + "\").style." + String(exec[2]) + "=\"" + String(exec[3]) + "\";");
										}break;
										case 'fade':{
											ExternalInterface.call("opacityFade",HTMLElementId,String(exec[5]));
										}break;
										case 'call':{
											ExternalInterface.call(String(exec[2]),String(exec[3]),String(exec[4]),String(exec[5]));
										}break;
									}
								}
								var lyricLine:String = String(lrc[String(i)]).replace(/^\{.+\}/g,"");
								lyricLine = lyricLine.replace(String.fromCharCode(13),"");
								trace(lyricLine.length);
								trace(lyricLine.charCodeAt(0));
								if(lyricLine.length>0){
									ExternalInterface.call("showLyrics",lyricLine);
								}
							}
						}else{
							ExternalInterface.call("showLyrics",lrc[String(i)]);
						}
					}
					lastPos=position;
				}
			}
			public function loadError():void{
				//Swallow it!
			}
		]]>
	</mx:Script>
	<mx:HTTPService id="fetchLRC" 
    	url="default.lrc"
    	result="lrcLoaded(event)"
    	fault="loadError()" 
    	resultFormat="text">
    </mx:HTTPService>
</mx:Application>

One Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注