智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

LRCParser组件

Posted on 2009-11-20 17:53  Bill Yuan  阅读(528)  评论(0编辑  收藏  举报
功能简述】:LRCParser组件是用来解析lrc歌词文件的代码类组件。
【运行平台】:Flash CS3,ActionScript3,Flash Player 9 +
【功能演示】: 【调用接口】:

 1 public interface com.klstudio.media.LRCParser {
 2     //获取lrc的偏移量(单位为毫秒数);
 3     public function getOffset():int;
 4 
 5     //设置lrc的偏移量;
 6     public function setOffset(offset:int):void;
 7 
 8     //获取lrc的歌名;
 9     public function getTitle():String;
10 
11     //获取lrc的歌手名;
12     public function getArtist():String;
13 
14     //获取lrc的专辑名;
15     public function getSpecial():String;
16 
17     //获取lrc文件的作者名;
18     public function getAuthor():String;
19 
20     //获取lrc的歌词列表;
21     public function getList():Array;
22 
23     //加载lrc文件,默认字符编码为GB2312;
24     public function load(url:String,charset:String="gb2312"):void;
25 }
26 

【使用说明】:
     1、使用前提条件:
         安装了Flash CS3软件;
         安装了 Adobe Extension Manager 1.8扩展管理器,如果没有请先到这里下载: http://www.adobe.com/cn/exchange/em_download/
         下载了LRCParser组件安装包文件: LRCParser.mxp
     2、打开LRCParser组件安装包,将LRCParser组件安装到Flash CS3里;
     3、将LRCParser组件拖到正在打开的fla文件的场景或库里,然后在调用代码(或类)的上面加上“import com.klstudio.media.LRCParser;”一行代码就可以了,接下来就"按照上面的接口和参考下面的调用实现"来调用这个解析器了。
【调用实例】:


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"90xFFFFFFtruenullnull"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(10x666666);
            base.graphics.beginFill(
0xF4F4F4);
            base.graphics.drawRect(
00, preWidth, preHeight - copyright.height - 10);

            base.graphics.lineStyle(
10x999999);
            base.graphics.moveTo(
025);
            base.graphics.lineTo(preWidth, 
25);
            base.graphics.moveTo(
1501);
            base.graphics.lineTo(
15025);
            base.graphics.moveTo(
2501);
            base.graphics.lineTo(
25025);

            base.addChild(getTextField(
"title""歌名:"new Rectangle(33147)));
            base.addChild(getTextField(
"artist""歌手:"new Rectangle(153397)));
            base.addChild(getTextField(
"special""专辑:"new Rectangle(2533147)));

            bmp 
= new Bitmap(new BitmapData(preWidth - 4, preHeight - 54false0));
            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 < arr.length)
            {
                
//时间数;
                
//要显示的歌词时间数是播放器时间数+歌词偏移量;
                var point:int = int(channel.position + parser.getOffset());
                
if (point <= arr[id + 1].time)
                {
                    drawLyric(arr[id].lyric);
                }
                
else
                {
                    id
++;
                }
            }
        }

        
//光晕滤镜;
        private function getBitmapFilter():BitmapFilter
        {
            var color:Number 
= 0x000000;
            var alpha:Number 
= 0.8;
            var blurX:Number 
= 2;
            var blurY:Number 
= 2;
            var strength:Number 
= 6;
            var inner:Boolean 
= false;
            var knockout:Boolean 
= false;
            var quality:Number 
= BitmapFilterQuality.HIGH;
            
return new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
        }

        
//lrc文件加载完成事件;
        private function completeHandler(event:Event):void
        {
            trace(
"completeHandler>>" + 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(00380), 0xFFCC00);
            bmp.bitmapData.applyFilter(bmp.bitmapData, 
new Rectangle(00, bmp.width, bmp.height), new Point(00), new ColorMatrixFilter([1000-50100-50010-500010]));
            bmp.bitmapData.applyFilter(bmp.bitmapData, 
new Rectangle(00, bmp.width, bmp.height), new Point(00), new BlurFilter(1212));
            var matrix:Matrix 
= new Matrix();
            matrix.translate(
1013);
            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(00, bmp.width, bmp.height), 0);
            id 
= 0;
            button.visible 
= true;
        }
    }

}