flash中显示LRC歌词原理

  • 内容
  • 相关
//--接口-- 
function loadLRC(path) { 
delete this.onEnterFrame; 
lrcData.timer = new Array(); 
lrcData.info = new Array(); 
myCodePage = System.useCodepage; 
System.useCodepage = true; 
isLRCShow = false; 
mc_lrc.tx_lrc.htmlText = " "; 
mc_lrc.tx_lrc.setTextFormat(lrcFormat); 
trace("LRC文件地址:"+path); 
lrcFile.load(path); 
mc_loadEvent.onEnterFrame = function() { 
if (lrcFile.getBytesLoaded()>10) { 
var per = Math.floor(lrcFile.getBytesLoaded()/lrcFile.getBytesTotal()*100); 
mc_lrc.tx_lrc.htmlText = "

"+"正在下载歌词.."+per+"%"; 
} else { 
mc_lrc.tx_lrc.htmlText = "

"+"正在查找歌词.."+""; 
} 
mc_lrc.tx_lrc.setTextFormat(lrcFormat); 
}; 
} 
// 
//读取LRC文本流 
lrcFile = new XML(); 
lrcFile.onLoad = function(success) { 
if (success) { 
trace(true); 
myLRC = this.toString(); 
myLRC = myLRC.slice(myLRC.indexOf("[")); 
initLRC(); 
} else { 
trace("LRC LoadError"); 
mc_lrc.tx_lrc.htmlText = "

"+"未加载歌词"+""; 
mc_lrc.tx_lrc.setTextFormat(lrcFormat); 
} 
delete mc_loadEvent.onEnterFrame; 
//编码恢复 
if (myCodePage != undefined) { 
System.useCodepage = myCodePage; 
} else { 
System.useCodepage = false; 
} 
}; 
// 
//LRC解析器 
myLRC = new String(); 
lrcData = new Object(); 
lrcData.timer = new Array(); 
lrcData.info = new Array(); 
lrcData.txScroll = new Array(); 
function initLRC() { 
i = 0; 
hasMoreTags = false; 
offset = 0; 
for (var k = 0; k<200; k++) { 
//LRC文本流分行 
var cur = myLRC.indexOf("\n"); 
parseLineData(myLRC.slice(0, cur)); 
myLRC = myLRC.slice(cur+1); 
if (myLRC.length<=0) { 
break; 
} 
} 
if (hasMoreTags == true) { 
sortTimer(); 
} 
trace(lrcData.timer); 
startShowLRC(); 
} 

function parseLineData(lineString) { 
var line
Data = new String(lineString); 
//解析[00:25.62]时间标签 
var startCharID = lineData.indexOf("["); 
if (isNaN(lineData.slice(startCharID+1, startCharID+3)) == false && lineData.indexOf(":") == startCharID+3) { 
//标准[00:25.62]格式 
if (lineData.indexOf("]") == startCharID+9) { 
lrcData.info[i] = lineData.slice(lineData.lastIndexOf("]")+1); 
lrcData.timer[i] = Number(lineData.slice(startCharID+1, startCharID+3))*60+Number(lineData.slice(startCharID+4, startCharID+6))+Number(lineData.slice(startCharID+7, startCharID+9)/100); 
} else if (lineData.indexOf("]") == startCharID+10) { 
//兼容[00:25.625]格式 
lrcData.info[i] = lineData.slice(lineData.lastIndexOf("]")+1); 
lrcData.timer[i] = Number(lineData.slice(startCharID+1, startCharID+3))*60+Number(lineData.slice(startCharID+4, startCharID+6))+Number(lineData.slice(startCharID+7, startCharID+10)/1000); 
} else if (lineData.indexOf("]") == startCharID+8) { 
//兼容[00:25.6]格式 
lrcData.info[i] = lineData.slice(lineData.lastIndexOf("]")+1); 
lrcData.timer[i] = Number(lineData.slice(startCharID+1, startCharID+3))*60+Number(lineData.slice(startCharID+4, startCharID+6))+Number(lineData.slice(startCharID+7, startCharID+8)/10); 
} else if (lineData.indexOf("]") == startCharID+6) { 
//兼容[00:21]格式 
lrcData.info[i] = lineData.slice(lineData.lastIndexOf("]")+1); 
lrcData.timer[i] = Number(lineData.slice(startCharID+1, startCharID+3))*60+Number(lineData.slice(startCharID+4, startCharID+6)); 
} 
} else { 
var tagName; 
//解析标识标签 
//[ar:艺人名] 
if (lineData.indexOf("[ar:") != -1) { 
tagName = "艺人:"; 
} else if (lineData.indexOf("[ti:") != -1) { 
//[ti:曲名] 
tagName = "歌曲:"; 
} else if (lineData.indexOf("[al:") != -1) { 
//[al:专辑名] 
tagName = "专辑:"; 
} else if (lineData.indexOf("[by:") != -1) { 
//[by:编辑LRC歌词的网友信息] 
tagName = "歌词制作:"; 
} 
if (lineData.lastIndexOf("]")>lineData.lastIndexOf("[")) { 
lrcData.info[i] = tagName+lineData.slice(lineData.lastIndexOf(":")+1, lineData.lastIndexOf("]")); 
} else { 
lrcData.info[i] = lineData; 
} 
if (lineData.indexOf("[offset:") != -1) { 
//[offset:时间补偿值] 
lrcData.info[i] = "------------------------------------------"; 
offset = Number(lineData.slice(lineData.lastIndexOf(":")+1, lineData.lastIndexOf("]"))); 
if (isNaN(offset)) { 
offset = 0; 
} 
} 
(歌词FLASH,谢谢!)
lrcData.timer[i] = 0; 
} 
i++; 
//[01:17][02:24][02:53][03:21] 多时间标签支持 
var surplusData = new String(lineData.slice(lineData.indexOf("]")+1)); 
if (surplusData.indexOf("[") != -1) { 
hasMoreTags = true; 
parseLineData(surplusData); 
} 
} 
function sortTimer() { 
lrcData.timerID = new Array(); 
for (var i = 0; i lrcData.timerID[i] = i; 
} 
////使用快速排序(备用) 
//QuickSorter(lrcData.timer); 
InsertionSorter(lrcData.timer); 
lrcData.tmpInfo = new Array(); 
for (var i = 0; i lrcData.tmpInfo[i] = lrcData.info[lrcData.timerID[i]]; 
} 
lrcData.info = lrcData.tmpInfo; 
} 
//数组排序算法:插入排序 
//这份算法代码核心是从网上找的 
function InsertionSorter(list) { 
for (var i = 1; i var t = list[i]; 
var j = i; 
while ((j>0) && (list[j-1]>t)) { 
list[j] = list[j-1]; 
var myC = lrcData.timerID[j]; 
lrcData.timerID[j] = lrcData.timerID[j-1]; 
lrcData.timerID[j-1] = myC; 
--j; 
} 
list[j] = t; 
} 
} 
//快速排序(备用) 
//这份算法代码核心是从网上找的 
function QuickSorter(list) { 
var low = 0; 
var high = list.length-1; 
if (high<=low) { 
return; 
} else if (high == low+1) { 
if (list[low]>list[high]) { 
Swap(low, high); 
return; 
} 
} 
myQuickSort(list, low, high); 
} 
function myQuickSort(list, low, high) { 
if (low var pivot = Partition(list, low, high); 
myQuickSort(list, low, pivot-1); 
myQuickSort(list, pivot+1, high); 
} 
} 
function Partition(list, low, high) { 
var pivot; 
var pivot = list[low]; 
while (low while (low=pivot) { 
high--; 
} 
if (low != high) { 
Swap(low, high); 
low++; 
} 
while (low low++; 
} 
if (low != high) { 
Swap(low, high); 
high--; 
} 
} 
return low; 
} 
function Swap(l, r) { 
var myl = lrcData.timerID[l]; 
lrcData.timerID[l] = lrcData.timerID[r]; 
lrcData.timerID[r] = myl; 
lrcData.timer[l] = lrcData.timer[l] ^ lrcData.timer[r]; 
lrcData.timer[r] = lrcData.timer[l] ^ lrcData.timer[r]; 
lrcData.timer[l] = lrcData.timer[l] ^ lrcData.timer[r]; 
} 
// 
//LRC显示 
function startShowLRC() { 
this.onEnterFrame = checTimer; 
isLRCShow = true; 
var t = getTimer(); 
var myTextTest = ""; 
for (var i = 0; i<=lrcData.info.length; i++) { 
myTextTest += lrcData.info[i]+"\n"; 
mc_lrc.tx_lrc.text = myTextTest; 
lrcData.txScroll[i] = mc_lrc.tx_lrc.maxscroll; 
} 
trace(getTimer()-t); 
trace(lrcData.txScroll); 
mc_lrc.tx_lrc.htmlText = ""; 
showLRCInfo(0); 
} 
t = 0; 
function checTimer() { 
if (_root.musicPlay.mySound.position-checBackPosition<-500) { 
t = 0; 
mc_lrc.tx_lrc.scroll = 0; 
} 
checBackPosition = _root.musicPlay.mySound.position; 
//时间检测 
if (_root.musicPlay.mySound.position>lrcData.timer[t]*1000-600-offset && lrcData.timer[t]>=lrcData.timer[t-1]) { 
if (lrcData.timer[t] != 0) { 
mc_lrc.tx_lrc.scroll = lrcData.txScroll[t-1]+3; 
} 
gotoAndPlay(8); 
n = t; 
t++; 
} 
} 
fontColor1 = new String("005CB9"); 
fontColor2 = new String("FFFFFF"); 
lrcFormat = new TextFormat(); 
lrcFormat.align = "center"; 
function showLRCInfo(id) { 
frontInfo = new String(); 
for (var i = 0; i frontInfo += lrcData.info[i]+" 
"; 
} 
backInfo = new String(); 
for (var i = id+1; i<=lrcData.info.length; i++) { 
backInfo += lrcData.info[i]+" 
"; 
} 
mc_lrc.tx_lrc.htmlText = ""+frontInfo+""+""+lrcData.info[id]+" 
"+""+backInfo+""; 
mc_lrc.tx_lrc.setTextFormat(lrcFormat); 
}

本文标签:

版权声明:若无特殊注明,本文皆为《鬼少》原创,转载请保留文章出处。

本文链接:flash中显示LRC歌词原理 - http://tv1314.com/post-145.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

00:00 / 00:00
顺序播放