JavaScript的歌词同步组件

很久没发文,一直想写点东西,整理整理这阵子的心得,很多笔记都在整理中。

最近给公司编写的一个JS UI,用于歌词同步,整理一下放出来,
核心脚本只负责处理lrc格式的歌词和呈现,并提供同步功能。

2008121112213068.jpg


lrc.jpg
外部呈现等均可以良好定制。
基本调用如下:
var lrc=new LRC({lyricTable:obj,lyricWrapper:obj,curRowClassName:'xx',lyric:'xxx',separator:'<BR>'});
if(lrc.IsLyricValid()) lrc.DoSync(60);

DoSync(t)用于同步,参数t为当前播放进度,从播放器获得。
IsLyricValid()返回歌词是否合法的LRC格式。

贴出代码,附件下载中包含2个定制示例。代码在FF下跑不了,因为音乐播放插件跑不了~

ContractedBlock.gif ExpandedBlockStart.gif Lrc UI
  1ExpandedBlockStart.gifContractedBlock.gif/**//*
  2InBlock.gif*   @author:    huangxu
  3InBlock.gif*   @date:      2008-11
  4InBlock.gif*   @site:  http://wsky.cnblogs.com
  5InBlock.gif*   @descript:  sync display the lyric 
  6InBlock.gif*   @usage:
  7InBlock.gif*               //import lrc.css
  8InBlock.gif*               var lrc=new LRC({lyricTable:obj,lyricWrapper:obj,curRowClassName:'xx',lyric:'xxx',separator:'<BR>'});
  9InBlock.gif*               if(lrc.IsLyricValid()) lrc.DoSync(60);
 10InBlock.gif*
 11InBlock.gif*   @note:  内部变量前缀lrc_,普通变量i,ii,index,arg..
 12ExpandedBlockEnd.gif*/

 13None.gif
 14ExpandedBlockStart.gifContractedBlock.gifLRC=function()dot.gif{this.initialize.apply(this,arguments);}
 15ExpandedBlockStart.gifContractedBlock.gifLRC.prototype=dot.gif{
 16InBlock.gif    arrLyricTime:[],
 17InBlock.gif    arrLyric:[],//全局可用,必须执行初始化
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    initialize:function(arg)dot.gif{
 19InBlock.gif        //私有
 20InBlock.gif        this.lyricTable=arg.lyricTable;//目标歌词table
 21InBlock.gif        this.lyricWrapper=arg.lyricWrapper;//目标歌词容器div
 22InBlock.gif        this.curRowClassName=arg.curRowClassName;//选择行css样式名
 23InBlock.gif        this.separator=arg.separator;//歌词行分隔符 如:<BR>
 24InBlock.gif        //执行初始化
 25InBlock.gif        this.arrLyricTime=[];
 26InBlock.gif        this.arrLyric=[];
 27InBlock.gif        this.initArray(arg.lyric);
 28InBlock.gif        this.arrLyricTime=this.sort(this.arrLyricTime);
 29InBlock.gif        this.setLyricTable(this.arrLyric);
 30ExpandedSubBlockEnd.gif    }
,
 31ExpandedSubBlockStart.gifContractedSubBlock.gif    initArray:function(lyric)dot.gif{
 32InBlock.gif        var lrc_re=new RegExp('\[[0-9:.]*\]','g');//g全局标志,获取所有匹配结果必须
 33InBlock.gif        var lrc_arr=lyric.split(this.separator);
 34InBlock.gif        var lrc_temp=0;
 35InBlock.gif        var lrc_filter=0;//无效行过滤标记
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i=0;i<lrc_arr.length;i++)dot.gif{
 37InBlock.gif            var lrc_txt=lrc_arr[i].replace(/\[[\w\W]*\]/g,'').Trim();//add to lyric text array
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            if(lrc_txt=='')dot.gif{
 39InBlock.gif                lrc_filter++;
 40InBlock.gif                continue;
 41ExpandedSubBlockEnd.gif            }
       
 42InBlock.gif            this.arrLyric[i-lrc_filter]=lrc_txt;
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            while((lrc_result = lrc_re.exec(lrc_arr[i])) != null)dot.gif{
 44InBlock.gif                var lrc_second=this.parseSecond(lrc_result.toString().replace(/\[|\]/g,''));
 45InBlock.gif                if(!isNaN(lrc_second))
 46InBlock.gif                    this.arrLyricTime[lrc_temp++]=(i-lrc_filter)+'|'+lrc_second;//arrLyricTime格式为"行号|秒",如:1|50,2|60
 47ExpandedSubBlockEnd.gif            }

 48ExpandedSubBlockEnd.gif        }

 49ExpandedSubBlockEnd.gif    }
,
 50InBlock.gif    /////
 51InBlock.gif    //  公开函数 
 52InBlock.gif    //  IsLyricValid()判断是否合法lrc歌词    
 53InBlock.gif    //  DoSync()定位歌词
 54InBlock.gif    /////
 55ExpandedSubBlockStart.gifContractedSubBlock.gif    IsLyricValid:function(arrLyricTime)dot.gif{
 56InBlock.gif        return this.arrLyricTime.length>0;
 57ExpandedSubBlockEnd.gif    }
,
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    DoSync:function(curPosition)dot.gif{
 59InBlock.gif        var lrc_times=this.arrLyricTime;
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i=0;i<lrc_times.length;i++)dot.gif{
 61InBlock.gif            var lrc_arrPre=lrc_times[i].split('|');
 62InBlock.gif            
 63InBlock.gif            if(i==0&&curPosition<lrc_arrPre[1]) break;//防止抖动
 64InBlock.gif            
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            if(lrc_times[i+1]==undefined)dot.gif{
 66InBlock.gif                this.setRow(lrc_arrPre[0]);
 67InBlock.gif                break;
 68ExpandedSubBlockEnd.gif            }

 69InBlock.gif            
 70InBlock.gif            var lrc_arrNext=lrc_times[i+1].split('|');
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            if(curPosition>=lrc_arrPre[1]&&curPosition<lrc_arrNext[1])dot.gif{
 72InBlock.gif                this.setRow(lrc_arrPre[0]);
 73InBlock.gif                break;
 74ExpandedSubBlockEnd.gif            }

 75ExpandedSubBlockEnd.gif        }
 
 76ExpandedSubBlockEnd.gif    }
,
 77InBlock.gif    /////
 78InBlock.gif    //以下为内部辅助函数
 79InBlock.gif    /////
 80ExpandedSubBlockStart.gifContractedSubBlock.gif    parseSecond:function(time)dot.gif{
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        trydot.gif{
 82InBlock.gif            var lrc_arr=time.split(':');//time格式为时间格式 00:00
 83InBlock.gif            return parseInt(lrc_arr[0])*60+parseFloat(lrc_arr[1]);
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(ex)dot.gif{
 85InBlock.gif            return 0;
 86ExpandedSubBlockEnd.gif        }

 87ExpandedSubBlockEnd.gif    }
,
 88ExpandedSubBlockStart.gifContractedSubBlock.gif    setLyricTable:function(arrLyric)dot.gif{
 89InBlock.gif        this.lyricWrapper.scrollTop=0;//滚动条置顶
 90InBlock.gif        
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        if(this.lyricTable.rows.length>0)dot.gif
 92InBlock.gif            this.clearTable(this.lyricTable);
 93ExpandedSubBlockEnd.gif        }

 94ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i=0;i<arrLyric.length;i++)dot.gif{
 95InBlock.gif            var lrc_tr=this.lyricTable.insertRow();
 96InBlock.gif            var lrc_cell=lrc_tr.insertCell(0);
 97InBlock.gif            lrc_cell.innerHTML=arrLyric[i];
 98ExpandedSubBlockEnd.gif        }

 99ExpandedSubBlockEnd.gif    }
,
100ExpandedSubBlockStart.gifContractedSubBlock.gif    clearTable:function(lyricTable)dot.gif{
101InBlock.gif        var lrc_rowNum=lyricTable.rows.length;
102ExpandedSubBlockStart.gifContractedSubBlock.gif        for (var i=0;i<lrc_rowNum;i++)dot.gif{
103InBlock.gif            lyricTable.deleteRow(i);
104InBlock.gif            lrc_rowNum=lrc_rowNum-1;
105InBlock.gif            i=i-1;
106ExpandedSubBlockEnd.gif        }
 
107ExpandedSubBlockEnd.gif    }
,
108ExpandedSubBlockStart.gifContractedSubBlock.gif    setRow:function(index)dot.gif{
109InBlock.gif        this.setRowClass(index);
110InBlock.gif        this.setScroll(index);
111ExpandedSubBlockEnd.gif    }
,
112ExpandedSubBlockStart.gifContractedSubBlock.gif    setRowClass:function(index)dot.gif{
113InBlock.gif        var lrc_rows=this.lyricTable.rows;
114ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i=0;i<lrc_rows.length;i++)dot.gif{
115InBlock.gif             lrc_rows[i].className='';//TODO:直接添加样式至元素,防止外部css干扰
116ExpandedSubBlockEnd.gif        }

117InBlock.gif        lrc_rows[index].className=this.curRowClassName;
118ExpandedSubBlockEnd.gif    }
,
119ExpandedSubBlockStart.gifContractedSubBlock.gif    setScroll:function(index)dot.gif{
120InBlock.gif        this.lyricWrapper.scrollTop=this.lyricTable.rows[index].offsetTop-this.lyricWrapper.offsetHeight/3;
121ExpandedSubBlockEnd.gif    }
,
122ExpandedSubBlockStart.gifContractedSubBlock.gif    sort:function(arrLyricTime)dot.gif{
123ExpandedSubBlockStart.gifContractedSubBlock.gif        for(var i=0;i<arrLyricTime.length-1;i++)dot.gif{
124ExpandedSubBlockStart.gifContractedSubBlock.gif            for(var ii=i+1;ii<arrLyricTime.length;ii++)dot.gif{
125InBlock.gif                var lrc_cur=parseFloat(arrLyricTime[i].split('|')[1]);
126InBlock.gif                var lrc_next=parseFloat(arrLyricTime[ii].split('|')[1]);
127ExpandedSubBlockStart.gifContractedSubBlock.gif                if(lrc_cur>lrc_next)dot.gif{
128InBlock.gif                    var lrc_temp=arrLyricTime[i];
129InBlock.gif                    arrLyricTime[i]=arrLyricTime[ii];
130InBlock.gif                    arrLyricTime[ii]=lrc_temp;
131ExpandedSubBlockEnd.gif                }

132ExpandedSubBlockEnd.gif            }

133ExpandedSubBlockEnd.gif        }
    
134InBlock.gif        return arrLyricTime;
135ExpandedSubBlockEnd.gif    }

136ExpandedBlockEnd.gif}

137None.gif
138None.gif
139None.gif/////
140None.gif//外部函数
141None.gif/////
142None.gifString.prototype.Trim = function()
143ExpandedBlockStart.gifContractedBlock.gifdot.gif{    
144InBlock.gif    return this.replace(/^\s*|\s*$/g,"");
145ExpandedBlockEnd.gif}

146None.gif

 

Demo下载:http://files.cnblogs.com/wsky/lrcUI_wsky.rar

转载于:https://www.cnblogs.com/wsky/archive/2008/12/11/1352666.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值