AS3同步LRC歌词的MP3播放器.
【组件版本】:0.5
【功能简述】:LRCParser组件是用来解析lrc歌词文件的代码类组件。
【运行平台】:Flash CS3,ActionScript3,Flash Player 9 +
【功能演示】:
【调用接口】:
- public interface com.klstudio.media.LRCParser {
- //获取lrc的偏移量(单位为毫秒数);
- public function getOffset():int;
- //设置lrc的偏移量;
- public function setOffset(offset:int):void;
- //获取lrc的歌名;
- public function getTitle():String;
- //获取lrc的歌手名;
- public function getArtist():String;
- //获取lrc的专辑名;
- public function getSpecial():String;
- //获取lrc文件的作者名;
- public function getAuthor():String;
- //获取lrc的歌词列表;
- public function getList():Array;
- //加载lrc文件,默认字符编码为GB2312;
- public function load(url:String,charset:String="gb2312"):void;
- }
【使用说明】:
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",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<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(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;
- }
- }
- }
实例文件打包下载:http://www.klstudio.com/demo/test/test4.rar (由于mp3比较大,所以就没有打包进去,下载到点这里) ;
来自:http://www.klstudio.com/post/128.html