[AS3]LRC类-用于加载和处理lrc歌词

Submitted by kinglong on 2007-3-26 17:51:12

[AS3]LRC类
LRC类,用于加载和处理lrc歌词文件,参考了网上的一些代码,并新加和优化了代码!
[ActionScript版本]  ActionScript3
[Flash  Player版本] Flash 9.0

LRC类

  1. package com.klstudio.media{   
  2.            
  3.         import flash.net.URLRequest;   
  4.         import flash.net.URLStream;   
  5.         import flash.events.*;   
  6.            
  7.         import com.klstudio.util.StringUtil;   
  8.         import com.klstudio.data.map.HashMap;   
  9.            
  10.         public class LRC extends EventDispatcher{   
  11.                 public static const COMPLETE:String = "complete";   
  12.                 public static const ERROR:String = "error";   
  13.                 private var stream:URLStream;   
  14.                 private var charset:String;   
  15.                 private var offset:int;   
  16.                 private var title:String;   
  17.                 private var artist:String;   
  18.                 private var special:String;   
  19.                 private var author:String;   
  20.                 private var loaded:Boolean;   
  21.                 private var list:Array;   
  22.                    
  23.                 function LRC(){   
  24.                         this.loaded = false;   
  25.                         this.offset = 0;   
  26.                         this.title = "";   
  27.                         this.artist = "";   
  28.                         this.special = "";   
  29.                         this.author = "";   
  30.                         this.list = new Array();   
  31.                         this.stream = new URLStream();   
  32.                         this.configureURLStreamListeners(this.stream);   
  33.                 }   
  34.                    
  35.                 public function getOffset():int{   
  36.                         return this.offset;   
  37.                 }   
  38.                 public function setOffset(offset:int):void{   
  39.                         this.offset = offset;   
  40.                 }   
  41.                 public function getTitle():String{   
  42.                         return this.title;   
  43.                 }   
  44.                 public function getArtist():String{   
  45.                         return this.artist;   
  46.                 }   
  47.                 public function getSpecial():String{   
  48.                         return this.special;   
  49.                 }   
  50.                 public function getAuthor():String{   
  51.                         return this.author;   
  52.                 }   
  53.                 public function getList():Array{   
  54.                         return this.list;   
  55.                 }   
  56.                 public function load(url:String,charset:String="gb2312"):void{   
  57.                         this.offset = 0;   
  58.                         this.title = "";   
  59.                         this.artist = "";   
  60.                         this.special = "";   
  61.                         this.author = "";   
  62.                         this.loaded = false;   
  63.                         this.list = new Array();   
  64.                         var request:URLRequest = new URLRequest(url);   
  65.                         this.charset = charset;   
  66.                         try {   
  67.                                 this.stream.load(request);   
  68.                         } catch (error:Error) {   
  69.                                 dispatchEvent(new Event(LRC.ERROR));   
  70.                         }   
  71.                 }   
  72.                 private function configureURLStreamListeners(dispatcher:IEventDispatcher):void {   
  73.                         dispatcher.addEventListener(Event.COMPLETE, completeHandler);   
  74.                         dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);   
  75.                         dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorHandler);   
  76.                         dispatcher.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);   
  77.                 }   
  78.                 private function completeHandler(event:Event):void{   
  79.                         var str:String = this.stream.readMultiByte(this.stream.bytesAvailable,this.charset);   
  80.                         var arr:Array = str.split("\r\n");   
  81.                         this.deleteWhitespace(arr);   
  82.                         this.readHeadInfo(arr);   
  83.                         this.deCompress(arr);   
  84.                         this.loaded = true;   
  85.                         dispatchEvent(new Event(LRC.COMPLETE));   
  86.                 }   
  87.                 private function errorHandler(event:Event):void{   
  88.                         dispatchEvent(new Event(LRC.ERROR));   
  89.                 }   
  90.                 private function deleteWhitespace(a:Array):void{   
  91.                         for(var i:uint=0;i<a.length;i++){   
  92.                                 a[i] = a[i].split("]");   
  93.                                 if(a[i].length < 2 ){   
  94.                                         a.splice(i,1);   
  95.                                         i--;   
  96.                                 }else{   
  97.                                         if(StringUtil.trim(a[i][1]).length == 0){   
  98.                                                 a[i][1] = "";   
  99.                                         }   
  100.                                         for(var j:uint=0;j<a[i].length;j++){   
  101.                                                 var ja:Array = a[i][j].split("[");   
  102.                                                 if(ja.length > 1){   
  103.                                                         ja.shift();   
  104.                                                 }   
  105.                                                 a[i][j] = ja[0];   
  106.                                         }   
  107.                                 }   
  108.                         }   
  109.                 }   
  110.                 private function readHeadInfo(a:Array):void{   
  111.                         for(var i:uint=0;i<a.length;i++){   
  112.                                 var ia:Array = a[i][0].split(":");   
  113.                                 switch(ia[0]){   
  114.                                         case "ar":   
  115.                                         this.artist = ia[1];   
  116.                                         a.splice(i,1);   
  117.                                         i--;   
  118.                                         break;   
  119.                                         case "ti":   
  120.                                         this.title = ia[1];   
  121.                                         a.splice(i,1);   
  122.                                         i--;   
  123.                                         break;   
  124.                                         case "al":   
  125.                                         this.special = ia[1];   
  126.                                         a.splice(i,1);   
  127.                                         i--;   
  128.                                         break;   
  129.                                         case "by":   
  130.                                         this.author = ia[1];   
  131.                                         a.splice(i,1);   
  132.                                         i--;   
  133.                                         break;   
  134.                                         case "offset":   
  135.                                         this.offset = parseInt(ia[1]);   
  136.                                         a.splice(i,1);   
  137.                                         i--;   
  138.                                         break;   
  139.                                 }   
  140.                         }   
  141.                 }   
  142.                 private function deCompress(a:Array):void{   
  143.                         for(var i:uint=0;i<a.length;i++){   
  144.                                 for(var j:uint=0;j<a[i].length-1;j++){   
  145.                                         this.list.push({time:LRC.toTime(a[i][j]),lyric:a[i][a[i].length-1]});   
  146.                                 }   
  147.                         }   
  148.                         delete a;   
  149.                         this.list.sortOn("time",Array.NUMERIC);   
  150.                 }   
  151.                 private static function toTime(str:String):int{   
  152.                         var a:Array = str.split(":");   
  153.                         var ia:Array = a[1].split(".");   
  154.                         if(ia.length < 2){   
  155.                                 ia.push("00");   
  156.                         }   
  157.                         var result:int = parseInt(a[0])*60000;   
  158.                         result += parseInt(ia[0])*1000;   
  159.                         result += parseInt(ia[1])*10;   
  160.                         return result;   
  161.                 }   
  162.                    
  163.         }   
  164. }   
  165.   


TestLRC类-是LRC类的测试用类

  1. package{   
  2.         import flash.display.Sprite;   
  3.         import flash.events.*;   
  4.         import flash.net.URLRequest;   
  5.         import flash.media.Sound;   
  6.         import flash.media.SoundChannel;   
  7.         import flash.utils.Timer;   
  8.            
  9.            
  10.         import com.klstudio.media.LRC;   
  11.         public class TestLRC extends Sprite{   
  12.                 private var lrc:LRC;   
  13.                 private var snd:Sound;   
  14.                 private var channel:SoundChannel;   
  15.                 private var id:uint = 0;   
  16.                 function TestLRC(){   
  17.                         this.initTime();   
  18.                         this.initLRC();   
  19.                         this.drawDisp();   
  20.                         this.initSound();   
  21.                 }   
  22.                 private function drawDisp():void{   
  23.                         var disp:LRCDisplay = new LRCDisplay();   
  24.                         disp.x = 50;   
  25.                         disp.y = 50;   
  26.                         disp.name = "disp";   
  27.                         this.addChild(disp);   
  28.                 }   
  29.                 private function initLRC():void{   
  30.                         this.lrc = new LRC();   
  31.                         lrc.addEventListener(LRC.COMPLETE,this.completeHandler);   
  32.                         lrc.load("天使的翅膀.lrc");   
  33.                 }   
  34.                 private function initSound():void{   
  35.                         snd = new Sound();   
  36.                         snd.load(new URLRequest('天使的翅膀.mp3'));   
  37.                            
  38.                         channel = snd.play();   
  39.                         channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);   
  40.                         positionTimer.start();   
  41.                 }   
  42.                 private function initTime():void{   
  43.                         positionTimer = new Timer(50);   
  44.                         positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);   
  45.                 }   
  46.                 private function completeHandler(event:Event):void{   
  47.                         trace("complete");   
  48.                         var arr:Array = event.target.getList();   
  49.                         with(this.getChildByName("disp")){   
  50.                                 show0_txt.text = "";   
  51.                                 show1_txt.text = "";   
  52.                                 show2_txt.text = "";   
  53.                                 show3_txt.text = arr[0].lyric;   
  54.                                 show4_txt.text = arr[1].lyric;   
  55.                         }   
  56.                 }   
  57.                    
  58.                 private function positionTimerHandler(event:Event):void{   
  59.                         var arr:Array = this.lrc.getList();   
  60.                         if(id<arr.length){   
  61.                                 var point:int = int(channel.position+this.lrc.getOffset());   
  62.                                 if(point <= arr[id+1].time){   
  63.                                         with(this.getChildByName("disp")){   
  64.                                                 show0_txt.text = ((id-2)>=0)?arr[id-2].lyric:"";   
  65.                                                 show1_txt.text = ((id-1)>=0)?arr[id-1].lyric:"";   
  66.                                                 show2_txt.text = arr[id].lyric;   
  67.                                                 show3_txt.text = ((id+1) < arr.length)?arr[id+1].lyric:"";   
  68.                                                 show4_txt.text = ((id+2) < arr.length)?arr[id+2].lyric:"";   
  69.                                         }   
  70.                                 }else{   
  71.                                         id ++;   
  72.                                 }   
  73.                         }   
  74.                 }   
  75.                    
  76.                 private function soundCompleteHandler(event:Event):void {   
  77.                         trace("soundCompleteHandler: " + event);   
  78.                         positionTimer.stop();   
  79.                 }   
  80.                    
  81.         }   
  82. }   
  83.