package project.test { import flash.display.*; import flash.events.*; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import flash.text.*; import flash.filters.*; import flash.net.URLRequest; import flash.media.Sound; import flash.media.SoundChannel; import flash.utils.Timer; //导入系统组件; import fl.controls.Button; //导入LRCParser类; import com.klstudio.media.LRCParser; public class Test4 extends Sprite{ //定义解析器; private var parser:LRCParser; //定义声音对象; private var snd:Sound; //定义声音播放频道; private var channel:SoundChannel; //歌词序号; private var id:uint = 0; //定时器; private var timer:Timer; //显示平台; private var base:Sprite; //歌词显示; private var bmp:Bitmap; //播放按钮; private var button:Button; private var preWidth:uint; private var preHeight:uint; private var copyright:TextField; public function Test4(){ preWidth = 400; preHeight = 100; stage.scaleMode = StageScaleMode.NO_SCALE; stage.showDefaultContextMenu = false; //显示平台; base = new Sprite(); addChild(base); //初始化解析器; parser = new LRCParser(); parser.addEventListener(Event.COMPLETE,completeHandler); parser.addEventListener(ErrorEvent.ERROR,errorHandler); parser.load("/demo/test/爱情转移.lrc"); //初始化定时器; timer = new Timer(50); timer.addEventListener(TimerEvent.TIMER,timerHandler); //初始化声音对象; snd = new Sound(); snd.load(new URLRequest("/demo/test/爱情转移.mp3")); //copyright; copyright = new TextField(); copyright.autoSize = TextFieldAutoSize.LEFT; copyright.selectable = false; copyright.defaultTextFormat = new TextFormat("Verdana",9,0xFFFFFF,true,null,null,"http://www.klstudio.com","_blank"); copyright.text = "POWERED BY KINGLONG"; copyright.x = preWidth - copyright.width - 5; copyright.y = preHeight - copyright.height - 5; var filter:BitmapFilter = getBitmapFilter(); copyright.filters = [filter]; copyright.alpha = 0.8; addChild(copyright); //显示平台; base.graphics.lineStyle(1,0x666666); base.graphics.beginFill(0xF4F4F4); base.graphics.drawRect(0,0,preWidth,preHeight - copyright.height - 10); base.graphics.lineStyle(1,0x999999); base.graphics.moveTo(0,25); base.graphics.lineTo(preWidth,25); base.graphics.moveTo(150,1); base.graphics.lineTo(150,25); base.graphics.moveTo(250,1); base.graphics.lineTo(250,25); base.addChild(getTextField("title","歌名:",new Rectangle(3,3,147))); base.addChild(getTextField("artist","歌手:",new Rectangle(153,3,97))); base.addChild(getTextField("special","专辑:",new Rectangle(253,3,147))); bmp = new Bitmap(new BitmapData(preWidth-4,preHeight-54,false,0)); bmp.x = 2; bmp.y = 27; addChild(bmp); button = new Button(); button.x = (preWidth - button.width)/2; button.y = 40; button.label = "PLAY MUSIC"; button.enabled = false; button.addEventListener(MouseEvent.CLICK, clickHandler); addChild(button); } //按钮点击事件; private function clickHandler(event:MouseEvent):void{ //声音播放频道 if(channel != null){ channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); channel = null; } channel = snd.play(); channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); timer.start(); button.visible = false; } //获得文本框; private function getTextField(name:String,lbl:String,rect:Rectangle,clr:uint=0x666666):TextField{ var txf:TextField = new TextField(); txf.width = rect.width; txf.selectable = false; txf.defaultTextFormat = new TextFormat("Courier New",12,clr); txf.name = name; txf.text = lbl; txf.x = rect.x; txf.y = rect.y; return txf; } //定时器间隔事件; private function timerHandler(event:TimerEvent):void{ //获取歌词列表; var arr:Array = parser.getList(); if(id>"+event); var title:TextField = base.getChildByName("title") as TextField; var artist:TextField = base.getChildByName("artist") as TextField; var special:TextField = base.getChildByName("special") as TextField; title.text = "歌名:" + parser.getTitle(); artist.text = "歌手:" + parser.getArtist(); special.text = "专辑:" + parser.getSpecial(); button.enabled = true; } //显示歌词; private function drawLyric(lyric:String):void{ var txf:TextField = getTextField("lyric",lyric,new Rectangle(0,0,380),0xFFCC00); bmp.bitmapData.applyFilter(bmp.bitmapData,new Rectangle(0,0,bmp.width,bmp.height),new Point(0,0),new ColorMatrixFilter([1,0,0,0,-5,0,1,0,0,-5,0,0,1,0,-5,0,0,0,1,0])); bmp.bitmapData.applyFilter(bmp.bitmapData,new Rectangle(0,0,bmp.width,bmp.height),new Point(0,0),new BlurFilter(12,12)); var matrix:Matrix = new Matrix(); matrix.translate(10,13); bmp.bitmapData.draw(txf,matrix,null,BlendMode.SCREEN); } //lrc文件加载失败事件; private function errorHandler(event:ErrorEvent):void{ trace("errorHandler>>"+event); } //歌曲播放完成事件; private function soundCompleteHandler(event:Event):void { trace("soundCompleteHandler: " + event); if(timer.running){ timer.stop(); } bmp.bitmapData.fillRect(new Rectangle(0,0,bmp.width,bmp.height),0); id = 0; button.visible = true; } } }