网页播放器动态显示歌词的代码

这是网页播放器动态显示歌词的代码(修改自百度MP3试听播放器代码),像百度一样单曲播放没有任何问题,但连播时持续播放10多首歌后CPU占用就超过90%了,直到死机
以下是代码:

[Copy to clipboard]

CODE:

function lrcInterface(pID,lrcID){
var tmpOutput=null;
var bdLRC=null;
var count=null;
var tmp=null;
var lrcFileName=null;
var lrcfile_xmlhttp=null;
var tmpURL=null;
var tmpStr=null;tmpOutput=document.getElementById(‘followTd’);
count=0;
tmpOutput.innerHTML=”

正在加载歌词信息,请您稍等片刻….”;
tmp=setInterval(getObject,200);
function getObject(){
bdLRC=new bdSyncLRC();
if(bdLRC!=null){
clearInterval(tmp);

if(typeof(pID)==”string”){
bdLRC.playerObj=document.getElementById(pID);
}else{
if(typeof(pID)==”object”)bdLRC.playerObj=pID;
}

lrcFileName=lrcID+”.lrc”;
bdLRC.lrcURL=”http://192.168.0.249/music/readlrc.asp?checked=”+lrcFileName;
bdLRC.lrcShower=”followTd”;

lrcfile_xmlhttp=obj_newxmlhttp();
tmpURL=bdLRC.lrcURL;
tmpStr=””;
lrcfile_xmlhttp.onreadystatechange=function(){
if(lrcfile_xmlhttp.readyState==4){
tmpStr=bytes2BSTR(lrcfile_xmlhttp.responseBody);
lrcfile_xmlhttp=null;
bdLRC.lrcContent=tmpStr;
}
}
lrcfile_xmlhttp.open(“POST”,tmpURL,true);
lrcfile_xmlhttp.send(null);

bdLRC.begin();
}else{
if(count==25){
clearInterval(tmp);
tmpOutput.innerHTML=”

可能是因为网络的原因,系统没有找到合适的歌词。请稍后重试”;
}else count++;
}
}
}
function bdSyncLRC(){
var playerObj=null;
var playerTypeStr=null;
var lrcURL=null;
var lrcShower=null;
var lrcContent=null;
var lrcObjArray=null;
lrcURL=””;
lrcShower=””;
lrcContent=””;
lrcObjArray=new Array();
this.preLRC=new Array();
this.offsetTime=0;
this.scrollMoveLen=20;
}
bdSyncLRC.prototype.getPlayer=function(){
if(typeof(document.getElementById(“this.playerObj”))==’object’){
return this.playerObj;
}else{
return null;
}
}
bdSyncLRC.prototype.getURL=function(){return this.lrcURL;};
bdSyncLRC.prototype.getOutput=function(){
if(this.lrcShower!=””){
if(typeof(this.lrcShower)==”string”){
return document.getElementById(this.lrcShower);
}else{
if(typeof(this.lrcShower)==”object”)return this.lrcShower;
}
};
return null;
};
bdSyncLRC.prototype.multiLRC=function(lrcLine){
thisObj=this;
tmpVar=lrcLine.split(“]”);
if(tmpVar.length>=2){
lrcText=tmpVar[tmpVar.length-1];
for(j3=0;j3<tmpVar.length-1;j3++){
lrcTime=tmpVar[j3]+”]”;
thisObj.preLRC.push(lrcTime+””+lrcText);
}
}
}
bdSyncLRC.prototype.preSyncLRC=function(){
if(this.lrcContent==null||this.lrcContent==””)return;
lrcLines=this.lrcContent.split(“\n”);
tmpArr=new Array();
for(i1=0;i1<lrcLines.length;i1++)this.multiLRC(replaceStr(lrcLines[i1])); this.preLRC.sort(); var tmpVar=”aaa”; for(i2=this.preLRC.length-1;i2>=0;i2–){
if(this.preLRC[i2]==tmpVar){
tmpVar=this.preLRC[i2];
this.preLRC.splice(i2,1);
}else{
tmpVar=this.preLRC[i2];
}
}
preTime=0;
indexLRC=1;
tmpArr[0]=new LRCItem(0,””);
for(i3=0;i3<this.preLRC.length;i3++){ if(this.preLRC[i3].length>9){
tmpTime=getLyrcTime(this.preLRC[i3]);
tmpLrc=getLyrc(this.preLRC[i3]);
if(tmpTime<preTime)continue; preTime=tmpTime; var tmpItem=new LRCItem(tmpTime,tmpLrc); tmpArr[indexLRC++]=tmpItem; } } var tmpItem=new LRCItem(3600,”Over…”); tmpArr[indexLRC]=tmpItem; if(tmpArr.length>=1)this.lrcObjArray=tmpArr;
}
function replaceStr(tmpStr){return tmpStr.replace(/^\s*|\s*$/g,””);};
function getLyrc(tmpStr){try{tmpArray=tmpStr.split(“]”);return tmpArray[tmpArray.length-1];}catch(e){};return””;};
function getLyrcTime(tmpStr){try{tmpChar=tmpStr.split(“]”)[0].split(“[“)[1];tmpVar=tmpChar.split(“:”);if(tmpVar.length<2)return 0;min=tmpVar[1].split(“.”)[0];tmpInt=parseInt(tmpVar[0])*60+parseFloat(min);if(!isNaN(tmpInt))return tmpInt;}catch(e){};return 0;}; function LRCItem(){varlrcTime=-1;var lrcContent=””;if(arguments.length>=2){this.lrcTime=arguments[0];this.lrcContent=arguments[1];};};
function obj_newxmlhttp(){
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlhttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e2) {
xmlhttp = false;
}
}
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’) {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
bdSyncLRC.prototype.begin=function(){
thisObj=this;
tmpPlayerObj=thisObj.getPlayer();
var tmpOutput=document.getElementById(‘followTd’);
var tmpI=setInterval(getlrc,100);
var count1=0;
function getlrc(){
if(typeof(thisObj.lrcContent)!=”undefined”){
clearInterval(tmpI);
thisObj.preSyncLRC();
var tmp,tmpArray;
tmpArray=thisObj.lrcObjArray;
tmp=”
“+”
“+”
“+”
“+”
“+”
“+”
“;
for(i10=0;i10<tmpArray.length;i10++)tmp+=”“+replaceStr(tmpArray[i10].lrcContent)+”
“;
thisObj.getOutput().innerHTML=tmp;
getPlayingTime(tmpPlayerObj,thisObj);
}else{
if(count1==50){
clearInterval(tmpI);
tmpOutput.innerHTML=”

可能是因为网络的原因,系统没有找到合适的歌词。请稍后重试”;
}else count1++;
}
}
}
function getPlayingTime(playerObj,bdLRCObj){
try{
tmpbdLRCObj=bdLRCObj;
var tmpPlayerObj=playerObj;
if(typeof(tmpPlayerObj)==”string”)tmpPlayerObj=document.getElementById(tmpPlayerObj);
window.setTimeout(“getPlayingTime(tmpPlayerObj,tmpbdLRCObj)”,500);
bdsyncLyrc(tmpbdLRCObj);
}catch(e){
document.title=”getPlayingTime…”+e.toString();
}
}
var preMatchLrcLine=-1;
function bdsyncLyrc(bdLRCObj){
try{
tmpPlayer=bdLRCObj.getPlayer();
playerStatus=tmpPlayer.GetPlayState();
if(tmpPlayer){
var tmpLrcContent=null;
var color=null;
var lineCount=null;
var perLineCount=null;
var i4=null;
currentTime=tmpPlayer.GetPosition()/1000;
// bdLRCObj.preSyncLRC();
tmpLrcs=bdLRCObj.lrcObjArray;
tmpScroll=bdLRCObj.getOutput();
tmpScrollId=bdLRCObj.getOutput();
perLength=bdLRCObj.scrollMoveLen;
tmpLrcContent=new Array(20);
color=new Array(21);
lineCount=new Array(tmpLrcs.length);
perLineCount=new Array(tmpLrcs.length);
for(i4=0;i4<tmpLrcs.length;i4++){
lineCount[i4]=Math.floor(tmpLrcs[i4].lrcContent.length/67);
perLineCount[i4]=0;
for(j1=0;j1<i4;j1++){
perLineCount[i4]+=lineCount[j1];
}
}
//window.upid6.innerHTML=perLineCount;
color[0]=”#000000″;color[1]=”#0d0500″;color[2]=”#1a0a00″;color[3]=”#270f00″;color[4]=”#341400″;color[5]=”#411900″;color[6]=”#4e1e00″;color[7]=”#5b2300″;color[8]=”#682800″;color[9]=”#752d00″;color[10]=”#823200″;color[11]=”#8f3700″;color[12]=”#9c3c00″;color[13]=”#a94100″;color[14]=”#b64600″;color[15]=”#c34b00″;color[16]=”#d05000″;color[17]=”#dd5500″;color[18]=”#ea5a00″;color[19]=”#f75f00″;color[20]=”#ff5a00″;
for(i5=0;i5<20;i5++)tmpLrcContent[i5]=”
“+”
“+”
“+”
“+”
“+”
“+”
“;
var tmpMatchLrcLine=0;
for(j2=0;j2<tmpLrcs.length;j2++){
nowLrc=tmpLrcs[j2];
nextLrc=tmpLrcs[(j2<tmpLrcs.length-1)?j2+1:j2];
nowTime=nowLrc.lrcTime+bdLRCObj.offsetTime;
nextTime=nextLrc.lrcTime+bdLRCObj.offsetTime;
if(nowTime<=currentTime&¤tTime<nextTime){
tmpMatchLrcLine=j2;
for(i6=0;i6<20;i6++){
tmpLrcContent[i6]+=”“+replaceStr(tmpLrcs[j2].lrcContent)+”
“;
}
window.upid6.innerHTML=”“+replaceStr(tmpLrcs[j2].lrcContent)+”“;
j2+=1;
tmpInt=tmpLrcs.length-j2;
for(;j2<tmpLrcs.length;j2++){
for(i7=0;i7<20;i7++){
tmpLrcContent[i7]+=”“+replaceStr(tmpLrcs[j2].lrcContent)+”
“;
}
}
if(tmpInt<8){
for(j2=0;j2<=8-tmpInt;j2++){
for(i8=0;i8<20;i8++){
tmpLrcContent[i8]+=”
“;
}
}
}
break;
}else{
//window.upid6.innerHTML=preMatchLrcLine;
if(j2==preMatchLrcLine){
for(i9=0;i9<20;i9++)tmpLrcContent[i9]+=”“+replaceStr(tmpLrcs[j2].lrcContent)+”
“;
} else {
for(i0=0;i0<20;i0++)tmpLrcContent[i0]+=”“+replaceStr(tmpLrcs[j2].lrcContent)+”
“;
}
}
}
if(preMatchLrcLine!=tmpMatchLrcLine){
if(Math.abs(tmpMatchLrcLine-preMatchLrcLine)>1){
tmpScroll.innerHTML=tmpLrcContent[19];
tmpScrollId.scrollTop=(tmpMatchLrcLine+perLineCount[tmpMatchLrcLine]+1)*perLength;
// tmpScrollId.scrollTop=(tmpMatchLrcLine+perLineCount[tmpMatchLrcLine]+1)*perLength;
} else {
var count2=0;
function scrollAdd(){
tmpScroll.innerHTML=tmpLrcContent[2*count2];
tmpScrollId.scrollTop+=2*(1+lineCount[preMatchLrcLine]);
count2++;
if(count2==10)clearInterval(loaderInterval);
}
var loaderInterval=setInterval(scrollAdd,25);
}
}
preMatchLrcLine=tmpMatchLrcLine;
}
}catch(e){
document.title=”bdsyncLyrc=”+e.toString();
}
};

发表评论

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