Chinaunix首页 | 论坛 | 博客
  • 博客访问: 634328
  • 博文数量: 17
  • 博客积分: 248
  • 博客等级: 二等列兵
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-26 12:13
个人简介

别让往昔的悲伤和对未来的恐惧,毁了你当下的幸福。

文章分类

全部博文(17)

文章存档

2013年(6)

2012年(2)

2011年(9)

我的朋友

分类: Python/Ruby

2012-06-14 16:11:47

最近想买在做网站需要实现滚动歌词的效果,网上收集了下资料,改了下成功实现。项目采用了spring+mvc+mysql+jquery的架构,项目管理用的maven,在这我简化了代码,就一个jsp能实现效果,至于jplayer的使用上官网看看文档或者上网找找资料也就OK了,下面是jsp的代码:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"
  3.     pageEncoding="UTF-8"%>
  4. <%@ taglib uri="" prefix="c" %>
  5. <%@ taglib uri="" prefix="fmt" %>
  6. <%@ taglib prefix="form" uri="" %>
  7. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' ''>
  8. <html xmlns='' lang='en' xml:lang='en'>
  9. <head>

  10. <title></title>
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  12. <link href="skin/jplayer.blue.monday.css" rel="stylesheet" type="text/css" />

  13. <script type="text/javascript" src="js/jquery.js"></script>
  14. <script type="text/javascript" src="js/jquery.jplayer.min.js"></script>
  15. <script type="text/javascript" src="js/player.js"></script>
  16. <script type="text/javascript">
  17. var playTime;
  18. window.lrc_obj = null;//评论对象
  19. $(document).ready(function() {
  20.     
  21.     $("#jquery_jplayer_1").jPlayer({
  22.         ready: function () {
  23.             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]围着欲望与理想";
  24.             change_lrc(lrc);
  25.             $(this).jPlayer("setMedia", {
  26.                 //mp3: '${rescFileServerUrl}${song.rescLoc}',
  27.                 mp3: 'media/cc.mp3',
  28.                 oga: ""
  29.             }).jPlayer("play");
  30.         },
  31.         
  32.         timeupdate : function (obj){
  33.             show_lrc(obj.jPlayer.status.currentTime);
  34.             playTime = obj.jPlayer.status.currentTime;
  35.         },
  36.         
  37.         ended: function (event) {
  38.             $(this).jPlayer("play");
  39.         },

  40.         swfPath: "js",
  41.         solution: "flash, html",
  42.         supplied: "ogv, m4v, oga, mp3, wav, m4a, webma"
  43.     });
  44. });

  45. //把歌词转换成时间和歌词内容的一个数组
  46. function change_lrc (data)
  47. {
  48.      //alert(data);
  49.   window.lrc_item = -1;//重置歌词下标
  50.   $('#lrc_panel').html('歌词解析中...');
  51.   var lrc = [{time:0, lrc:''}];
  52.   var match = data.match(/\[ *ti *\:([^\[\]]+)\]/i);
  53.   if (match) lrc[0].lrc += '歌名:' + match[1] + "
    "
    ;//歌名
  54.   
  55.   var match = data.match(/\[ *ar *\:([^\[\]]+)\]/i);
  56.   if (match) lrc[0].lrc += '词:' + match[1] + "
    "
    ;//作词
  57.   
  58.   var match = data.match(/\[ *cm *\:([^\[\]]+)\]/i);
  59.   if (match) lrc[0].lrc += '曲:' + match[1] + "
    "
    ;//作曲
  60.   
  61.   
  62.   var match = data.match(/\[ *al *\:([^\[\]]+)\]/i);
  63.   if (match) lrc[0].lrc += '  专辑:' + match[1] + "
    "
    ;//专辑
  64.   
  65.   var match = data.match(/\[ *sr *\:([^\[\]]+)\]/i);
  66.   if (match) lrc[0].lrc += '  歌手:' + match[1] + "
    "
    ;//歌手
  67.  
  68.   
  69.   var offset = 0;
  70.   var match = data.match(/\[ *offset *\:(\d+)\]/i);
  71.   if (match) offset = match[1] / 1000;//时间调整
  72.   
  73.   
  74.   var match = data.match(/(\[ *[\d\:\.]+ *\][^\[\]]+)/g);
  75.   if (match){
  76.     for (var i = 0; i < match.length; i++){
  77.       //alert(match[i]);
  78.       var temp = match[i].split(']');
  79.       var temp_lrc = temp[1];
  80.       temp = temp[0].replace('[', '');
  81.       temp = temp.split(':');
  82.       var time = 0;
  83.       time += temp[temp.length - 1] * 1;//
  84.       if (temp.length > 1) time += temp[temp.length - 2] * 60;///
  85.       if (temp.length > 2) time += temp[temp.length - 3] * 3600;///
  86.       
  87.       if (time != 0) time += offset;//只有非0歌词才有必要加调整;
  88.       //alert(time + ':' + temp_lrc);
  89.       
  90.       if (lrc[lrc.length -1].time == time) lrc[lrc.length -1].lrc += '  ' + temp_lrc + "
    "
    ;//相同时间合并
  91.       else lrc[lrc.length] = {time:time, lrc:temp_lrc + "
    "
    };//按获取顺序记录,如果lrc是乱序将导致后面显示错误
  92.     }
  93.   }
  94.   
  95.   window.lrc_obj = lrc;//处理好再复制
  96.   //alert(lrc[57].lrc);
  97.   show_lrc (0);//显示歌曲信息
  98. }

  99. /*
  100.  * 显示歌词,并得到正在播放歌词的下标,
  101.  * param:sec 歌词对应的秒.
  102.  */
  103. function show_lrc (sec){
  104.      if (!window.lrc_obj) return 0;
  105.     
  106.      if ( window.lrc_obj[window.lrc_obj.length-1].time < sec ){
  107.      if (window.lrc_item < window.lrc_obj.length){
  108.      window.lrc_item = window.lrc_obj.length;
  109.      center_lrc(window.lrc_obj.length - 1);//显示尾句
  110.      }
  111.     
  112.      return 0;//显示到最后一句
  113.      }else if (window.lrc_item < 0){//初始化,且需要刷新歌词
  114.      window.lrc_item = 1;
  115.      center_lrc(0);//显示第一句
  116.     
  117.      if (sec < window.lrc_obj[window.lrc_item].time) return 0;//处于第二句前
  118.      }
  119.     
  120.      if ( (window.lrc_obj[window.lrc_item - 1].time <= sec) && (sec < window.lrc_obj[window.lrc_item].time) ){
  121.      return 0;//当前时间处于当前显示句之后,后一句之前,无需要刷新歌词
  122.      }
  123.     
  124.      if (window.lrc_obj[window.lrc_item].time < sec){//歌词过时了,自动播放或拉进度到后面
  125.      do
  126.      {
  127.      window.lrc_item++;
  128.      }while ( (window.lrc_item <= window.lrc_obj.length) && (window.lrc_obj[window.lrc_item].time < sec) )
  129.     
  130.      center_lrc(window.lrc_item - 1);//显示后面对应一句
  131.      return 0;
  132.      }else{//歌词过快,如拉前了
  133.      do
  134.      {
  135.      window.lrc_item--;
  136.      }while ( (0 <= window.lrc_item) && (sec < window.lrc_obj[window.lrc_item].time) )
  137.     
  138.      window.lrc_item++;
  139.      center_lrc(window.lrc_item - 1);//显示前面对应一句
  140.      return 0;
  141.     
  142.      }
  143. }

  144. /*
  145.  * 得到正在播放的歌词
  146.  * index 是数组的下标值.
  147.  */
  148. function center_lrc(index)
  149. {
  150.       if (! window.lrc_obj || (index >= window.lrc_obj.length ) || (index < 0) ) return 0;
  151.       var padding = 10;//居中头尾个数
  152.       var lrc_html = '';
  153.  
  154.       /* for (var i = 1; i <= padding ; i++){
  155.            if (index - i >= 0){
  156.             lrc_html += '
  157. ' + window.lrc_obj[index-i].lrc + '
  158. ';
  159.            }else lrc_html += '
  160. ';//不足补空,点位
  161.       }
  162.       var focusLrc = window.lrc_obj[index].lrc;
  163.       
  164.       //alert('"' + foucusLrc + '"');
  165.       if(focusLrc == '
    '
    ) {
  166.         lrc_html += '
  167. ........
  168. ';
  169.       } else {
  170.           lrc_html += '
  171. ' + focusLrc + '
  172. ';
  173.       }
  174.       
  175.       for (var i = 1; i <= padding ; i++){
  176.          if (index + i < window.lrc_obj.length){
  177.            //lrc_html += '
  178. [' + (index + i) + '|' + parseInt(window.lrc_obj[index+i].time) + ']' + window.lrc_obj[index+i].lrc + '
  179. ';
  180.            lrc_html += '
  181. ' + window.lrc_obj[index+i].lrc + '
  182. ';
  183.           }else lrc_html += '
  184. ';//不足补空,点位
  185.       } */
  186.       
  187.       for(var m = 0; m <= window.lrc_obj.length - 1; m++) {
  188.          if(index != m) {
  189.             
  190.              if(window.lrc_obj[m].lrc == '
    '
    ) {
  191.          lrc_html += '
  192. ........
  193. ';
  194.      } else {
  195.          lrc_html += '
  196. ' + window.lrc_obj[m].lrc + '
  197. ';
  198.      }
  199.          } else if(index == m) {
  200.             
  201.              if(window.lrc_obj[m].lrc == '
    '
    ) {
  202.                 lrc_html += '
  203. ........
  204. ';
  205.               } else {
  206.                   lrc_html += '
  207. ' + window.lrc_obj[m].lrc + '
  208. ';
  209.               }
  210.            }
  211.       }
  212.       //var lp = document.getElementById('lyrics');
  213.       //lp.scrollTop=lp.scrollHeight;
  214.       
  215.       $('#lrc_panel').get(0).innerHTML = lrc_html;
  216.       var lp = document.getElementById("lyrics");
  217.       var lh = $("#crly").position().top;
  218.       lp.scrollTop+=parseInt(lh) - 80;
  219. }

  220. </script>
  221. </head>
  222. <body>
  223.     

  224.     <div id="jquery_jplayer_1" class="jp-jplayer"></div>

  225.         <div class="jp-video-270p">
  226.             <div class="jp-type-single">
  227.                 <div id="jp_interface_1" class="jp-interface">
  228.                     <ul class="jp-controls">
  229.                         <li><a href="#" class="jp-play" tabindex="1">play</a></li>
  230.                         <li><a href="#" class="jp-pause" tabindex="1">pause</a></li>



  231.                     </ul>








  232.                 </div>
  233.             </div>
  234.         </div>
  235.         </div>          
  236.     <div class="lyrics" id="lyrics" style="overflow: scroll; width: 255px; height: 275px;">
  237.            <strong></strong> 
  238.            <ul id="lrc_panel" ></ul>
  239.     </div>
  240. </body>
  241. </html>


  • 歌词显示有两种方式:1.歌词全部显示,播放的歌词高亮,有个scroll自动滚动的问题,2.只显示歌词前后几句,没有scroll的问题,
    阅读(5915) | 评论(3) | 转发(0) |
    给主人留下些什么吧!~~

    ze_lin_huang2013-04-20 18:02:41

    fooklook:我想问一下,为什么在火狐浏览器上就不能播放mp3格式的音乐呢?

    播放mp3格式的音乐好像要在服务器环境才可以,还有注意swfPath: "<%=path%>/Jplayer/js",路径是否正确

    回复 | 举报

    guarx2013-04-19 12:21:42

    fooklook:我想问一下,为什么在火狐浏览器上就不能播放mp3格式的音乐呢?

    可以的,你打开fireBug看看有没有错误信息。

    回复 | 举报

    fooklook2013-04-12 08:37:56

    我想问一下,为什么在火狐浏览器上就不能播放mp3格式的音乐呢?