签到天数: 7 天 [LV.3]偶尔看看II 鲜花( 0) 鸡蛋( 0)
|
楼主 |
发表于 2018-12-10 22:18
|
显示全部楼层
LrcReader.as类
新建类LrcReader.as
代码如下
package {
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.utils.ByteArray;
import flash.xml.*;
public class LrcReader extends EventDispatcher {
private var byte:ByteArray;
private var lrcStr:String;
private var timeArr:Array;
private var lrcArr:Array;
private var reg:RegExp;
private var lrccode:String;
private var gs:String;
private var xmld:XML;
//实例化(文件路径,编码 )
public function LrcReader( url:String = null , code:String = "黑体" ) {
byte = new ByteArray();
lrcStr="";
timeArr=[];
lrcArr=[];
reg=/[\n\r]/;
lrccode=code;
if( url != null ){
var loader:URLLoader = new URLLoader();
var urlrq:URLRequest = new URLRequest( url );
if( url.slice( url.length - 3 , url.length ) != "xml" ){
loader.dataFormat = URLLoaderDataFormat.BINARY;
gs = "txt";
}else{
gs = "xml";
}
loader.load( urlrq );
loader.addEventListener(Event.COMPLETE , loadComplete );
}
}
//歌词加载完成
private function loadComplete( e:Event ):void {
if( gs == "txt" ){
byte = e.target.data as ByteArray;
lrcStr = byte.readMultiByte( byte.bytesAvailable , lrccode );
strToData( lrcStr );
}else{
xmld = new XML( e.target.data );
xmlToData( xmld );
}
}
//分析歌词
public function strToData( strdata:String ){
timeArr=redTime(strdata,"[","]");
lrcArr=redLrc(strdata,"]");
breakFinish();
}
//分析xml歌词
public function xmlToData( xm:XML ){
for( var xl:int = 0; xl < xm.Lyrics.Lrc.length() ; xl ++ ){
timeArr.push( int( Number( xm.Lyrics.Lrc[ xl ].@Tag ) / 10 ));
if( xm.Lyrics.Lrc[ xl ] == "" ){
lrcArr.push("-" );
}else{
lrcArr.push( xm.Lyrics.Lrc[ xl ] );
}
}
breakFinish();
}
//分解完毕
private function breakFinish(){
if (timeArr.length>0&&lrcArr.length>0&&timeArr.length==lrcArr.length) {
this.dispatchEvent( new Event ("complete"));
} else {
this.dispatchEvent( new Event ("error"));
trace( timeArr.length );
trace( lrcArr.length );
}
}
//读取并分析歌词文件里的时间
private function redTime( obj:String , fst:String , end:String ):Array {
var arr:Array=[];
var tim:Array=[];
var ih:Boolean=false;
var st:int=0;
var et:int=0;
var sr:String = "";
for (var i = 0; i < obj.length; i ++) {
if (obj.charAt(i)==fst) {
st=i;
ih=true;
}
if (obj.charAt(i)==end&&ih) {
et=i;
if (ih) {
ih=false;
if (isNaN(Number(obj.charAt(st+1)))==false) {
arr.push( obj.slice( st + 1 , et ) );
}else{
arr.push( "00:00.00" );
}
}
}
}
for (var a in arr) {
tim.push( strToNumber( arr[a] ));
}
return tim;
}
//将文本格式的时间转换成数字格式
private function strToNumber( strs:String ):Number {
var num:Array=[];
var nums:Number=0;
for (var s:int = 0 ; s < strs.length ; s++ ) {
if ( isNaN(Number(strs.charAt(s))) == false ) {
num.push( Number( strs.charAt( s ) ));
}
}
//歌词文件的时间模式为[01:59.99]
nums=num[5];
nums+=num[4]*10;
nums+=num[3]*100;
nums+=num[2]*1000;
nums+=num[1]*1000*6;
nums+=num[0]*1000*60;
return nums;
}
//读取并分析歌词文件里的歌词部分
private function redLrc( obj:String , fst:String ):Array {
var lra:Array=[];
var ih:Boolean=false;
var st:int=0;
var et:int=0;
var sr:String = "";
for (var i = 0; i < obj.length; i ++) {
if (obj.charAt(i)==fst) {
st=i;
ih = true;
}
if (ih) {
if (reg.test(obj.charAt(i))==true) {
sr = obj.slice( st + 1 , i );
if( sr.length == 0 ){
sr = " ";
}
lra.push(sr);
ih=false;
}
}
}
return lra;
}
//获取已分析的歌词数据
public function get lrcToArray():Array {
var lrcdata:Array=[];
if (timeArr.length==lrcArr.length) {
for (var j in timeArr) {
lrcdata.push([timeArr[j],lrcArr[j]]);
}
}
return lrcdata;
}
//获取文本形式的歌词数据
public function get lrcToString():String {
return lrcStr;
}
}
} |
|