Scripts 学盟
标题:
[JavaScript]网页中音乐LRC歌词同步显示
[打印本页]
作者:
Alvin
时间:
2011-4-13 23:26:05
标题:
[JavaScript]网页中音乐LRC歌词同步显示
2011-4-13 23:24:48 上传
下载附件
(31.95 KB)
function LRCPlayer(wmp, strLRC, container, width, height, color1, color2) {
this.wmp = wmp;
this.height = height;
this.checkInterval = 0;
this.transInterVal = 0;
this.currentFocus = -1;
var ls = this.ls = this.parseLRC(strLRC);
var div = document.createElement('div');
div.style.cssText = "display:block;width:"+width+"px; height:" + height + "px; overflow:hidden; background:#000;";
var lrcPanel = this.lrcPanel = document.createElement('div');
div.appendChild(lrcPanel);
container.appendChild(div);
this.colorTrans = this.colorCal(color1, color2);
this.color1 = color1;
var span = document.createElement('div');
span.style.cssText = "height:" + (height/2) + "px;";
lrcPanel.appendChild(span);
for (var i=0; i<ls.length; i++) {
span = document.createElement('div');
span.style.cssText = 'font-size:12px; height:16px; line-height:16px; text-align:center; overflow: hidden; white-space: nowrap; color:#' + color1;
span.appendChild(document.createTextNode(ls[i].text));
lrcPanel.appendChild(span);
}
span = document.createElement('div');
span.style.cssText = "height:" + (height/2) + "px;";
lrcPanel.appendChild(span);
this.start();
}
$.extend(LRCPlayer.prototype, {
start: function() {
var me = this;
!function() {
setTimeout(arguments.callee, 128);
me.checkPos();
}();
}
, stop: function() {
if (this.checkInterval)
clearTimeout(this.checkInterval);
}
, checkPos: function() {
try {
var cPos = this.wmp.controls.currentPosition;
if (isNaN(cPos)) return;
if (this.currentFocus<this.ls.length-1 && cPos > this.ls[this.currentFocus+1].pos) {
this.focusThis(this.currentFocus+1);
} else if (this.currentFocus>-1 && cPos<this.ls[this.currentFocus].pos) {
this.focusThis(this.currentFocus-1);
}
if (this.currentFocus >= this.ls.length-1) return;
var pos = this.ls[this.currentFocus].pos;
var nPos = this.ls[this.currentFocus+1].pos;
var lrct = (cPos - pos)/(nPos - pos);
lrct = Math.max(Math.min(lrct, 1), 0);
this.lrcPanel.parentNode.scrollTop = (this.currentFocus+lrct) * 16;
} catch(e) { }
}
, focusThis: function(index) {
if (this.transInterVal) {
clearTimeout(this.transInterVal);
}
if (this.currentFocus > -1) {
this.lrcPanel.childNodes[this.currentFocus+1].style.color = '#'+this.color1;
}
this.currentFocus = index;
var me = this;
var i = 0;
!function() {
if (i>=8) return;
me.lrcPanel.childNodes[me.currentFocus+1].style.color = '#' + me.colorTrans[i++];
me.transInterVal = setTimeout(arguments.callee, 128);
}();
}
, colorCal: function(c1, c2) {
var oC2 = c2;
c1 = parseInt(c1, 16); c2 = parseInt(c2, 16);
var r1 = c1 >> 16, g1 = (c1 >> 8) & 0xff; b1 = c1 & 0xff;
var r2 = c2 >> 16, g2 = (c2 >> 8) & 0xff; b2 = c2 & 0xff;
var cs = [], d1 = r2 - r1, d2 = g2 - g1, d3 = b2 - b1, r, g, b;
for (var i=1; i<8; i++) {
r = r1+parseInt(Math.round(d1*i*0.125, 0));
g = g1+parseInt(Math.round(d2*i*0.125, 0));
b = b1+parseInt(Math.round(d3*i*0.125, 0));
cs.push( ("00000"+((r<<16)|(g<<8)|b).toString(16)).slice(-6) );
}
return cs.concat(oC2);
}
, parseLRC: function(str) {
var map = {'ti':'标题:', 'ar':'艺术家:', 'al':'专辑:', 'by':'歌词制作:'};
str = str.replace(/\[(ti|ar|al|by):([^[\]]*)\]/ig, function($, $1, $2) {
return '[00:00.00]' + (map[$1.toLowerCase()]||'') + $2;
});
var offset = 0;
str = str.replace(/\[offset:(-?\d+)\]/gi, function($, $1) {
offset = parseInt($1, 10) * 0.001;
});
var regex1 = /(\[[^[\]]+\])(\[[^[\]]+\])([^[\]]*?)$/m;
var regex2 = /(\[[^[\]]+\])(\[[^[\]]+\])([^[\]]*?)$/mg;
while (regex1.test(str)) {
str = str.replace(regex2, "$1$3\n$2$3");
}
var ls = [];
str.replace(/\[(\d+):([\d.]+)\](.*)/g, function($, $1, $2, $3) {
ls.push({ pos:parseInt($1,10)*60+parseFloat($2,10)*1+offset, text:$3 });
});
ls.sort(function(a, b) {
return a.pos-b.pos;
});
return ls;
}
});
复制代码
具体使用例子,从下面地址下载:
http://u.115.com/file/f0faa8e20
附:
ASP 版的千千静听 LRC 歌词查询 [jscript]
http://www.iscripts.org/bbs/viewthread.php?tid=86
从千千静听的歌词服务器查询音乐的 LRC 歌词
http://www.iscripts.org/bbs/viewthread.php?tid=85
附件:
lrc.jpg
(2011-4-13 23:24:48, 31.95 KB) / 下载次数 301
http://www.iscripts.org/forum.php?mod=attachment&aid=N3xhN2FiNmIxZXwxNzM0NTU3MTM1fDB8MA%3D%3D
作者:
momo
时间:
2011-4-14 10:29:32
例子下载不了昂!
作者:
Alvin
时间:
2011-4-14 11:02:00
回复
2#
momo
可以的吧,U蛋的服务器难道坏了
作者:
Alvin
时间:
2011-4-14 11:06:34
那个电信下载的按钮,能下啊,我试了
作者:
momo
时间:
2011-4-14 11:13:50
回复
4#
Alvin
2011-4-14 11:13:06 上传
下载附件
(6.26 KB)
事实胜于雄辩
附件:
1.jpg
(2011-4-14 11:13:06, 6.26 KB) / 下载次数 238
http://www.iscripts.org/forum.php?mod=attachment&aid=OHwyZTc1ZmIwNXwxNzM0NTU3MTM1fDB8MA%3D%3D
作者:
Alvin
时间:
2011-4-14 11:16:21
u 蛋不给用迅雷的
作者:
那个谁
时间:
2011-4-15 10:44:52
作者:
momo
时间:
2011-4-15 11:37:29
回复
7#
那个谁
臭杰杰
哼哼哼
作者:
浴火凤凰
时间:
2011-4-15 11:51:00
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2