最近想买在做网站需要实现滚动歌词的效果,网上收集了下资料,改了下成功实现。项目采用了spring+mvc+mysql+jquery的架构,项目管理用的maven,在这我简化了代码,就一个jsp能实现效果,至于jplayer的使用上官网看看文档或者上网找找资料也就OK了,下面是jsp的代码:
- <?xml version="1.0" encoding="UTF-8" ?>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib uri="" prefix="c" %>
- <%@ taglib uri="" prefix="fmt" %>
- <%@ taglib prefix="form" uri="" %>
- <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' ''>
- <html xmlns='' lang='en' xml:lang='en'>
- <head>
- <title></title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link href="skin/jplayer.blue.monday.css" rel="stylesheet" type="text/css" />
- <script type="text/javascript" src="js/jquery.js"></script>
- <script type="text/javascript" src="js/jquery.jplayer.min.js"></script>
- <script type="text/javascript" src="js/player.js"></script>
- <script type="text/javascript">
- var playTime;
- window.lrc_obj = null;//评论对象
- $(document).ready(function() {
-
- $("#jquery_jplayer_1").jPlayer({
- ready: function () {
- var lrc="[ti:长城][ar:BEYOND][cm:]黄家驹[al:真的爱你][sr:BEYOND][00:00]长城[00:26][01:00]遥远的东方[01:01]辽阔的边疆[01:02]还有远古的破墙[01:06]前世的沧桑[01:07]后世的风光[01:09]万里千山牢牢接壤[01:12]围着老去的国度[01:15]围着事实的真相[01:18]围着浩瀚的岁月[01:21]围着欲望与理想";
- change_lrc(lrc);
- $(this).jPlayer("setMedia", {
- //mp3: '${rescFileServerUrl}${song.rescLoc}',
- mp3: 'media/cc.mp3',
- oga: ""
- }).jPlayer("play");
- },
-
- timeupdate : function (obj){
- show_lrc(obj.jPlayer.status.currentTime);
- playTime = obj.jPlayer.status.currentTime;
- },
-
- ended: function (event) {
- $(this).jPlayer("play");
- },
- swfPath: "js",
- solution: "flash, html",
- supplied: "ogv, m4v, oga, mp3, wav, m4a, webma"
- });
- });
- //把歌词转换成时间和歌词内容的一个数组
- function change_lrc (data)
- {
- //alert(data);
- window.lrc_item = -1;//重置歌词下标
- $('#lrc_panel').html('歌词解析中...');
- var lrc = [{time:0, lrc:''}];
- var match = data.match(/\[ *ti *\:([^\[\]]+)\]/i);
- if (match) lrc[0].lrc += '歌名:' + match[1] + "
";//歌名
-
- var match = data.match(/\[ *ar *\:([^\[\]]+)\]/i);
- if (match) lrc[0].lrc += '词:' + match[1] + "
";//作词
-
- var match = data.match(/\[ *cm *\:([^\[\]]+)\]/i);
- if (match) lrc[0].lrc += '曲:' + match[1] + "
";//作曲
-
-
- var match = data.match(/\[ *al *\:([^\[\]]+)\]/i);
- if (match) lrc[0].lrc += ' 专辑:' + match[1] + "
";//专辑
-
- var match = data.match(/\[ *sr *\:([^\[\]]+)\]/i);
- if (match) lrc[0].lrc += ' 歌手:' + match[1] + "
";//歌手
-
-
- var offset = 0;
- var match = data.match(/\[ *offset *\:(\d+)\]/i);
- if (match) offset = match[1] / 1000;//时间调整
-
-
- var match = data.match(/(\[ *[\d\:\.]+ *\][^\[\]]+)/g);
- if (match){
- for (var i = 0; i < match.length; i++){
- //alert(match[i]);
- var temp = match[i].split(']');
- var temp_lrc = temp[1];
- temp = temp[0].replace('[', '');
- temp = temp.split(':');
- var time = 0;
- time += temp[temp.length - 1] * 1;//秒
- if (temp.length > 1) time += temp[temp.length - 2] * 60;///分
- if (temp.length > 2) time += temp[temp.length - 3] * 3600;///时
-
- if (time != 0) time += offset;//只有非0歌词才有必要加调整;
- //alert(time + ':' + temp_lrc);
-
- if (lrc[lrc.length -1].time == time) lrc[lrc.length -1].lrc += ' ' + temp_lrc + "
";//相同时间合并
- else lrc[lrc.length] = {time:time, lrc:temp_lrc + "
"};//按获取顺序记录,如果lrc是乱序将导致后面显示错误
- }
- }
-
- window.lrc_obj = lrc;//处理好再复制
- //alert(lrc[57].lrc);
- show_lrc (0);//显示歌曲信息
- }
- /*
- * 显示歌词,并得到正在播放歌词的下标,
- * param:sec 歌词对应的秒.
- */
- function show_lrc (sec){
- if (!window.lrc_obj) return 0;
-
- if ( window.lrc_obj[window.lrc_obj.length-1].time < sec ){
- if (window.lrc_item < window.lrc_obj.length){
- window.lrc_item = window.lrc_obj.length;
- center_lrc(window.lrc_obj.length - 1);//显示尾句
- }
-
- return 0;//显示到最后一句
- }else if (window.lrc_item < 0){//初始化,且需要刷新歌词
- window.lrc_item = 1;
- center_lrc(0);//显示第一句
-
- if (sec < window.lrc_obj[window.lrc_item].time) return 0;//处于第二句前
- }
-
- if ( (window.lrc_obj[window.lrc_item - 1].time <= sec) && (sec < window.lrc_obj[window.lrc_item].time) ){
- return 0;//当前时间处于当前显示句之后,后一句之前,无需要刷新歌词
- }
-
- if (window.lrc_obj[window.lrc_item].time < sec){//歌词过时了,自动播放或拉进度到后面
- do
- {
- window.lrc_item++;
- }while ( (window.lrc_item <= window.lrc_obj.length) && (window.lrc_obj[window.lrc_item].time < sec) )
-
- center_lrc(window.lrc_item - 1);//显示后面对应一句
- return 0;
- }else{//歌词过快,如拉前了
- do
- {
- window.lrc_item--;
- }while ( (0 <= window.lrc_item) && (sec < window.lrc_obj[window.lrc_item].time) )
-
- window.lrc_item++;
- center_lrc(window.lrc_item - 1);//显示前面对应一句
- return 0;
-
- }
- }
- /*
- * 得到正在播放的歌词
- * index 是数组的下标值.
- */
- function center_lrc(index)
- {
- if (! window.lrc_obj || (index >= window.lrc_obj.length ) || (index < 0) ) return 0;
- var padding = 10;//居中头尾个数
- var lrc_html = '';
-
- /* for (var i = 1; i <= padding ; i++){
- if (index - i >= 0){
- lrc_html += '
- '
+ window.lrc_obj[index-i].lrc + ' ';
- }else lrc_html += '
';//不足补空,点位
- }
- var focusLrc = window.lrc_obj[index].lrc;
-
- //alert('"' + foucusLrc + '"');
- if(focusLrc == '
') {
- lrc_html += '
- ........
';
- } else {
- lrc_html += '
- '
+ focusLrc + ' ';
- }
-
- for (var i = 1; i <= padding ; i++){
- if (index + i < window.lrc_obj.length){
- //lrc_html += '
- ['
+ (index + i) + '|' + parseInt(window.lrc_obj[index+i].time) + ']' + window.lrc_obj[index+i].lrc + ' ';
- lrc_html += '
- '
+ window.lrc_obj[index+i].lrc + ' ';
- }else lrc_html += '
';//不足补空,点位
- } */
-
- for(var m = 0; m <= window.lrc_obj.length - 1; m++) {
- if(index != m) {
-
- if(window.lrc_obj[m].lrc == '
') {
- lrc_html += '
- ........
';
- } else {
- lrc_html += '
- '
+ window.lrc_obj[m].lrc + ' ';
- }
- } else if(index == m) {
-
- if(window.lrc_obj[m].lrc == '
') {
- lrc_html += '
- ........
';
- } else {
- lrc_html += '
- '
+ window.lrc_obj[m].lrc + ' ';
- }
- }
- }
- //var lp = document.getElementById('lyrics');
- //lp.scrollTop=lp.scrollHeight;
-
- $('#lrc_panel').get(0).innerHTML = lrc_html;
- var lp = document.getElementById("lyrics");
- var lh = $("#crly").position().top;
- lp.scrollTop+=parseInt(lh) - 80;
- }
- </script>
- </head>
- <body>
-
- <div id="jquery_jplayer_1" class="jp-jplayer"></div>
- <div class="jp-video-270p">
- <div class="jp-type-single">
- <div id="jp_interface_1" class="jp-interface">
- <ul class="jp-controls">
- <li><a href="#" class="jp-play" tabindex="1">play</a></li>
- <li><a href="#" class="jp-pause" tabindex="1">pause</a></li>
- </ul>
- </div>
- </div>
- </div>
- </div>
- <div class="lyrics" id="lyrics" style="overflow: scroll; width: 255px; height: 275px;">
- <strong></strong>
- <ul id="lrc_panel" ></ul>
- </div>
- </body>
- </html>
歌词显示有两种方式:1.歌词全部显示,播放的歌词高亮,有个scroll自动滚动的问题,2.只显示歌词前后几句,没有scroll的问题,
阅读(6048) | 评论(3) | 转发(0) |