使用LRC类加载和处理lrc歌词的方法
[AS3]LRC类
LRC类,用于加载和处理lrc歌词文件,参考了网上的一些代码,并新加和优化了代码!
[ActionScript版本] ActionScript3
[Flash Player版本] Flash 9.0
LRC类
代码:
package com.klstudio.media{
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.events.*;
import com.klstudio.util.StringUtil;
import com.klstudio.data.map.HashMap;
public class LRC extends EventDispatcher{
public static const COMPLETE:String = "complete";
public static const ERROR:String = "error";
private var stream:URLStream;
private var charset:String;
private var offset:int;
private var title:String;
private var artist:String;
private var special:String;
private var author:String;
private var loaded:Boolean;
private var list:Array;
function LRC(){
this.loaded = false;
this.offset = 0;
this.title = "";
this.artist = "";
this.special = "";
this.author = "";
this.list = new Array();
this.stream = new URLStream();
this.configureURLStreamListeners(this.stream);
}
public function getOffset():int{
return this.offset;
}
public function setOffset(offset:int):void{
this.offset = offset;
}
public function getTitle():String{
return this.title;
}
public function getArtist():String{
return this.artist;
}
public function getSpecial():String{
return this.special;
}
public function getAuthor():String{
return this.author;
}
public function getList():Array{
return this.list;
}
public function load(url:String,charset:String="gb2312"):void{
this.offset = 0;
this.title = "";
this.artist = "";
this.special = "";
this.author = "";
this.loaded = false;
this.list = new Array();
var request:URLRequest = new URLRequest(url);
this.charset = charset;
try {
this.stream.load(request);
} catch (error:Error) {
dispatchEvent(new Event(LRC.ERROR));
}
}
private function configureURLStreamListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, errorHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
}
private function completeHandler(event:Event):void{
var str:String = this.stream.readMultiByte(this.stream.bytesAvailable,this.charset);
var arr:Array = str.split("\r\n");
this.deleteWhitespace(arr);
this.readHeadInfo(arr);
this.deCompress(arr);
this.loaded = true;
dispatchEvent(new Event(LRC.COMPLETE));
}
private function errorHandler(event:Event):void{
dispatchEvent(new Event(LRC.ERROR));
}
private function deleteWhitespace(a:Array):void{
for(var i:uint=0;i<a.length;i++){
a[i] = a[i].split("]");
if(a[i].length < 2 ){
a.splice(i,1);
i--;
}else{
if(StringUtil.trim(a[i][1]).length == 0){
a[i][1] = "";
}
for(var j:uint=0;j<a[i].length;j++){
var ja:Array = a[i][j].split("[");
if(ja.length > 1){
ja.shift();
}
a[i][j] = ja[0];
}
}
}
}
private function readHeadInfo(a:Array):void{
for(var i:uint=0;i<a.length;i++){
var ia:Array = a[i][0].split(":");
switch(ia[0]){
case "ar":
this.artist = ia[1];
a.splice(i,1);
i--;
break;
case "ti":
this.title = ia[1];
a.splice(i,1);
i--;
break;
case "al":
this.special = ia[1];
a.splice(i,1);
i--;
break;
case "by":
this.author = ia[1];
a.splice(i,1);
i--;
break;
case "offset":
this.offset = parseInt(ia[1]);
a.splice(i,1);
i--;
break;
}
}
}
private function deCompress(a:Array):void{
for(var i:uint=0;i<a.length;i++){
for(var j:uint=0;j<a[i].length-1;j++){
this.list.push({time:LRC.toTime(a[i][j]),lyric:a[i][a[i].length-1]});
}
}
delete a;
this.list.sortOn("time",Array.NUMERIC);
}
private static function toTime(str:String):int{
var a:Array = str.split(":");
var ia:Array = a[1].split(".");
if(ia.length < 2){
ia.push("00");
}
var result:int = parseInt(a[0])*60000;
result += parseInt(ia[0])*1000;
result += parseInt(ia[1])*10;
return result;
}
}
}
TestLRC类-是LRC类的测试用类
代码:
package{
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.Timer;
import com.klstudio.media.LRC;
public class TestLRC extends Sprite{
private var lrc:LRC;
private var snd:Sound;
private var channel:SoundChannel;
private var id:uint = 0;
function TestLRC(){
this.initTime();
this.initLRC();
this.drawDisp();
this.initSound();
}
private function drawDisp():void{
var disp:LRCDisplay = new LRCDisplay();
disp.x = 50;
disp.y = 50;
disp.name = "disp";
this.addChild(disp);
}
private function initLRC():void{
this.lrc = new LRC();
lrc.addEventListener(LRC.COMPLETE,this.completeHandler);
lrc.load("天使的翅膀.lrc");
}
private function initSound():void{
snd = new Sound();
snd.load(new URLRequest('天使的翅膀.mp3'));
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
positionTimer.start();
}
private function initTime():void{
positionTimer = new Timer(50);
positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
}
private function completeHandler(event:Event):void{
trace("complete");
var arr:Array = event.target.getList();
with(this.getChildByName("disp")){
show0_txt.text = "";
show1_txt.text = "";
show2_txt.text = "";
show3_txt.text = arr[0].lyric;
show4_txt.text = arr[1].lyric;
}
}
private function positionTimerHandler(event:Event):void{
var arr:Array = this.lrc.getList();
if(id<arr.length){
var point:int = int(channel.position+this.lrc.getOffset());
if(point <= arr[id+1].time){
with(this.getChildByName("disp")){
show0_txt.text = ((id-2)>=0)?arr[id-2].lyric:"";
show1_txt.text = ((id-1)>=0)?arr[id-1].lyric:"";
show2_txt.text = arr[id].lyric;
show3_txt.text = ((id+1) < arr.length)?arr[id+1].lyric:"";
show4_txt.text = ((id+2) < arr.length)?arr[id+2].lyric:"";
}
}else{
id ++;
}
}
}
private function soundCompleteHandler(event:Event):void {
trace("soundCompleteHandler: " + event);
positionTimer.stop();
}
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |