package lyric {
import flash.utils.ByteArray;
import flash.net.URLLoaderDataFormat;
import flash.events.IOErrorEvent;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
/**
* @author jh7086
*/
public class Lyrics extends EventDispatcher {
private var loader : URLLoader;
private var primal_data : String;
private var $ head : Object;
private var $ lines : Array;
private var viewer : LyricsViewer;
private var audio : Audio;
function Lyrics() {
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_complete);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_IOError);
}
//加载歌词;
public function load(url : String) : void {
loader.load(new URLRequest(url));
}
//歌词加载完成;
private function loader_complete(event : Event) : void {
primal_data = decodeUTF8(loader.data);
primal_data = primal_data.substring(9, primal_data.length - 4);
trace(primal_data);
decode();
}
//歌词加载失败;
private function loader_IOError(event : IOErrorEvent) : void {
primal_data = "";
decode();
}
//将utf-8编码字符串转换为实际内容字符串;
//例如:%E6%B3%9B%E5%A4%AA%E9%82%B5,转换后为"泛太邵";
private function decodeUTF8(str : String) : String {
var result : String = str;
if(str.search(/(\%[0-9A-F]{2})+/i) != -1) {
var arr0 : Array = str.match(/(\%[0-9A-F]{2})+/ig);
var arr1 : Array;
var k : uint,i : uint;
var bt_arr : ByteArray;
for(k = 0;k < arr0.length;k++) {
arr1 = String(arr0[k]).match(/[0-9A-F]{2}/ig);
bt_arr = new ByteArray();
for(i = 0;i < arr1.length;i++) {
bt_arr.writeByte(Number("0x" + arr1[i]));
}
result = result.replace(/(\%[0-9A-F]{2})+/i, bt_arr.toString());
}
}
return result;
}
//解析歌词数据;
private function decode() : void {
//解析头部:[ti:言之尚早][ a r :周华健][al:雨人][by:http://liuxuan.com][offset:500]
$ head = {};
var i : String,j : String;
var arr : Array = primal_data.match(/\[[a-z ]+:[^\]]+\]/ig);
var temp : String;
var key : String;
var value : String;
for(i in arr) {
temp = arr[i];
temp = temp.substring(1, temp.length - 1);
key = String(temp.split(":")[0]).replace(" ", "");
value = temp.split(":")[1];
$ head[key] = value;
}
//解析歌词:[0][02:12.89][00:45.50]明晨或者心一软能让步[02:17.12][00:49.85]明年或者紧紧拥抱过不到
$ lines = [];
arr = primal_data.match(/(\[\d[\d:\.]*\])+[^\[]*/g);
var _arr : Array;
var _temp : String;
var time : int;
var lyrics : String;
for(i in arr) {
temp = arr[i];
_arr = temp.match(/\[\d[\d:\.]*\]/g);
//歌词,去掉时间标签,回车换行,&#??;实体
lyrics = temp.replace(/\[\d[\d:\.]*\]|\r|\n|&#[a-z0-9]+;/gi, "");
//处理同一歌词的多个时间标签
for(j in _arr) {
_temp = _arr[j];
_temp = _temp.substring(1, _temp.length - 1);
//时间,毫秒
time = offset + (Number(_temp.split(":")[0]) * 60 + Number(_temp.split(":")[1])) * 1000;
$ lines.push({time:time, lyrics:lyrics});
}
}
if($ lines.length > 0) {
$ lines.sortOn("time", Array.NUMERIC);
$ lines.unshift({time:$ lines[0]["time"], lyrics:""});
if(ar != "")$ lines.unshift({time:$ lines[0]["time"] / 2, lyrics:ar});
$ lines.unshift({time:0, lyrics:"<font size='15'><b>" + ti + "</b></font>"});
} else {
$ lines.push({time:0, lyrics:"暂无歌词"});
}
dispatchEvent(new Event(Event.COMPLETE));
}
public function setViewer(lyricsViewer : LyricsViewer) : void {
viewer = lyricsViewer;
viewer.lyrics = this;
}
public function setAudio(ad : Audio) : void {
audio = ad;
}
public function play() : void {
if($ lines != null && audio != null) {
viewer.data = $ lines;
viewer.audio = audio;
viewer.playLyric();
} else {
if($ lines == null) {
trace("Lyrics.play>>>歌词错误");
}
if(audio == null) {
trace("Lyrics.play>>>音频错误");
}
}
}
public function stop() : void {
viewer.stopLyric();
}
//时间补偿
private function get offset() : int {
return $ head["offset"] ? int($ head["offset"]) : 0;
}
//歌名
public function get ti() : String {
return $ head["ti"] ? $ head["ti"] : "";
}
//歌手
public function get ar() : String {
return $ head["ar"] ? $ head["ar"] : "";
}
//专辑
public function get al() : String {
return $ head["al"] ? $ head["al"] : "";
}
//歌词作者
public function get by() : String {
return $ head["by"] ? $ head["by"] : "";
}
}
}
import flash.utils.ByteArray;
import flash.net.URLLoaderDataFormat;
import flash.events.IOErrorEvent;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
/**
* @author jh7086
*/
public class Lyrics extends EventDispatcher {
private var loader : URLLoader;
private var primal_data : String;
private var $ head : Object;
private var $ lines : Array;
private var viewer : LyricsViewer;
private var audio : Audio;
function Lyrics() {
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_complete);
loader.addEventListener(IOErrorEvent.IO_ERROR, loader_IOError);
}
//加载歌词;
public function load(url : String) : void {
loader.load(new URLRequest(url));
}
//歌词加载完成;
private function loader_complete(event : Event) : void {
primal_data = decodeUTF8(loader.data);
primal_data = primal_data.substring(9, primal_data.length - 4);
trace(primal_data);
decode();
}
//歌词加载失败;
private function loader_IOError(event : IOErrorEvent) : void {
primal_data = "";
decode();
}
//将utf-8编码字符串转换为实际内容字符串;
//例如:%E6%B3%9B%E5%A4%AA%E9%82%B5,转换后为"泛太邵";
private function decodeUTF8(str : String) : String {
var result : String = str;
if(str.search(/(\%[0-9A-F]{2})+/i) != -1) {
var arr0 : Array = str.match(/(\%[0-9A-F]{2})+/ig);
var arr1 : Array;
var k : uint,i : uint;
var bt_arr : ByteArray;
for(k = 0;k < arr0.length;k++) {
arr1 = String(arr0[k]).match(/[0-9A-F]{2}/ig);
bt_arr = new ByteArray();
for(i = 0;i < arr1.length;i++) {
bt_arr.writeByte(Number("0x" + arr1[i]));
}
result = result.replace(/(\%[0-9A-F]{2})+/i, bt_arr.toString());
}
}
return result;
}
//解析歌词数据;
private function decode() : void {
//解析头部:[ti:言之尚早][ a r :周华健][al:雨人][by:http://liuxuan.com][offset:500]
$ head = {};
var i : String,j : String;
var arr : Array = primal_data.match(/\[[a-z ]+:[^\]]+\]/ig);
var temp : String;
var key : String;
var value : String;
for(i in arr) {
temp = arr[i];
temp = temp.substring(1, temp.length - 1);
key = String(temp.split(":")[0]).replace(" ", "");
value = temp.split(":")[1];
$ head[key] = value;
}
//解析歌词:[0][02:12.89][00:45.50]明晨或者心一软能让步[02:17.12][00:49.85]明年或者紧紧拥抱过不到
$ lines = [];
arr = primal_data.match(/(\[\d[\d:\.]*\])+[^\[]*/g);
var _arr : Array;
var _temp : String;
var time : int;
var lyrics : String;
for(i in arr) {
temp = arr[i];
_arr = temp.match(/\[\d[\d:\.]*\]/g);
//歌词,去掉时间标签,回车换行,&#??;实体
lyrics = temp.replace(/\[\d[\d:\.]*\]|\r|\n|&#[a-z0-9]+;/gi, "");
//处理同一歌词的多个时间标签
for(j in _arr) {
_temp = _arr[j];
_temp = _temp.substring(1, _temp.length - 1);
//时间,毫秒
time = offset + (Number(_temp.split(":")[0]) * 60 + Number(_temp.split(":")[1])) * 1000;
$ lines.push({time:time, lyrics:lyrics});
}
}
if($ lines.length > 0) {
$ lines.sortOn("time", Array.NUMERIC);
$ lines.unshift({time:$ lines[0]["time"], lyrics:""});
if(ar != "")$ lines.unshift({time:$ lines[0]["time"] / 2, lyrics:ar});
$ lines.unshift({time:0, lyrics:"<font size='15'><b>" + ti + "</b></font>"});
} else {
$ lines.push({time:0, lyrics:"暂无歌词"});
}
dispatchEvent(new Event(Event.COMPLETE));
}
public function setViewer(lyricsViewer : LyricsViewer) : void {
viewer = lyricsViewer;
viewer.lyrics = this;
}
public function setAudio(ad : Audio) : void {
audio = ad;
}
public function play() : void {
if($ lines != null && audio != null) {
viewer.data = $ lines;
viewer.audio = audio;
viewer.playLyric();
} else {
if($ lines == null) {
trace("Lyrics.play>>>歌词错误");
}
if(audio == null) {
trace("Lyrics.play>>>音频错误");
}
}
}
public function stop() : void {
viewer.stopLyric();
}
//时间补偿
private function get offset() : int {
return $ head["offset"] ? int($ head["offset"]) : 0;
}
//歌名
public function get ti() : String {
return $ head["ti"] ? $ head["ti"] : "";
}
//歌手
public function get ar() : String {
return $ head["ar"] ? $ head["ar"] : "";
}
//专辑
public function get al() : String {
return $ head["al"] ? $ head["al"] : "";
}
//歌词作者
public function get by() : String {
return $ head["by"] ? $ head["by"] : "";
}
}
}
回复Comments
作者:
{commentrecontent}