免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4130 | 回复: 1
打印 上一主题 下一主题

html5音乐播放器(chrome测试通过) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-02 14:32 |只看该作者 |倒序浏览
html5音乐播放器(chrome测试通过)











html5出来已经很久了,众所周知html5支持播放音乐和视频,作为一个html5网页的开发者,一直都想做一个html5的音乐播放器,同时也增加对javascript的认识,我利用空闲的一段时间开发了一个javascript版本的html5播放器,由于马上又有事情做,这个版本只具备的一部分的功能,欢迎大家一起交流,完成所有的功能。
靠,发现我太唐僧了,不说废话直接上代码了

Html代码
  1. 1.<!DOCTYPE html>  
  2. 2.<html>  
  3. 3.<head>  
  4. 4.<style type="text/css">  
  5. 5.button {   
  6. 6.    width: 80px;   
  7. 7.    height: 24px;   
  8. 8.}   
  9. 9.  
  10. 10.input[type="number"]{   
  11. 11.    width: 100px;   
  12. 12.    height: 24px;   
  13. 13.}   
  14. 14.  
  15. 15.ul{   
  16. 16.    list-style:none;   
  17. 17.}   
  18. 18.  
  19. 19.li{   
  20. 20.    list-style:none;   
  21. 21.}   
  22. 22.  
  23. 23.</style>  
  24. 24.</head>  
  25. 25.<body>  
  26. 26.    <div id="operations">  
  27. 27.        <div>  
  28. 28.            <button type="button" id="add" name="add">Add</button>  
  29. 29.        </div>  
  30. 30.        <div>  
  31. 31.            <button type="button" id="delete" name="delete">Delete</button>  
  32. 32.            <input type="number" placeholder="music num">  
  33. 33.        </div>  
  34. 34.        <div>  
  35. 35.            <button type="button" id="play" name="play">Play</button>  
  36. 36.        </div>  
  37. 37.        <div>  
  38. 38.            <button type="button" id="stop" name="stop">Stop</button>  
  39. 39.        </div>  
  40. 40.   </div>  
  41. 41.   <div>  
  42. 42.        <ul id="listNode">  
  43. 43.        </ul>  
  44. 44.   </div>  
  45. 45.   <script type="text/javascript">  
  46. 46.   (function(window, undifined){   
  47. 47.      
  48. 48.        var musicDiv = function(){   
  49. 49.               
  50. 50.            this._musicList = document.getElementsByTagName('audio');   
  51. 51.               
  52. 52.            this._musicEndedListener = function(){   
  53. 53.                this.currentTime = 0;   
  54. 54.                this.pause();   
  55. 55.                var currentId = this.getAttribute("id");   
  56. 56.                var arrId = currentId.split('_');   
  57. 57.                var nextMusic = document.getElementById(arrId[0] + "_" + (parseInt(arrId[1]) + 1));   
  58. 58.                if(nextMusic){   
  59. 59.                    nextMusic.play();   
  60. 60.                }else{   
  61. 61.                    document.getElementById(arrId[0] + "_" + "1").play();   
  62. 62.                }   
  63. 63.            };   
  64. 64.               
  65. 65.        };   
  66. 66.           
  67. 67.        musicDiv.prototype = {   
  68. 68.               
  69. 69.            initNo: function(startNo){   
  70. 70.                var numSpan = null;   
  71. 71.                var textNode = null;   
  72. 72.                var liNodes = document.getElementsByTagName('li');   
  73. 73.                for(var i = 1; i <= liNodes.length; i++){   
  74. 74.                    numSpan = document.createElement('span');   
  75. 75.                    textNode = document.createTextNode("No. " + i);   
  76. 76.                    numSpan.appendChild(textNode);   
  77. 77.                    if(!startNo){   
  78. 78.                        liNodes[i - 1].parentNode.insertBefore(numSpan, liNodes[i - 1]);   
  79. 79.                    }else{   
  80. 80.                        if(i >= startNo){   
  81. 81.                            liNodes[i - 1].parentNode.insertBefore(numSpan, liNodes[i - 1]);   
  82. 82.                        }   
  83. 83.                    }   
  84. 84.                }   
  85. 85.            },   
  86. 86.            initEvent: function(){   
  87. 87.                for(var i = 0; i < this._musicList.length; i++){   
  88. 88.                    this._musicList[i].removeEventListener('ended', this._musicEndedListener, false);   
  89. 89.                    this._musicList[i].addEventListener('ended',this._musicEndedListener);   
  90. 90.                }   
  91. 91.            }   
  92. 92.        };   
  93. 93.           
  94. 94.        var div = new musicDiv;   
  95. 95.        div.initNo();   
  96. 96.        div.initEvent();   
  97. 97.           
  98. 98.           
  99. 99.        var addMusic = function(){   
  100. 100.            var currentNo = document.getElementsByTagName('audio').length + 1;   
  101. 101.            var musicNode = null;   
  102. 102.            var sourceNode = null;   
  103. 103.            var liNode = null;   
  104. 104.            var listNode = document.getElementById('listNode');   
  105. 105.            var musicPath = prompt("Please copy the full path of music(mp3 only) in your disk:");   
  106. 106.            if(musicPath){   
  107. 107.                liNode = document.createElement('li');   
  108. 108.                musicNode = document.createElement('audio');   
  109. 109.                musicNode.setAttribute('id', 'music_' +  currentNo);   
  110. 110.                musicNode.setAttribute('controls', 'controls');   
  111. 111.                sourceNode = document.createElement('source');   
  112. 112.                sourceNode.setAttribute('src', musicPath);   
  113. 113.                sourceNode.setAttribute('type', 'audio/mpeg');   
  114. 114.                musicNode.appendChild(sourceNode);   
  115. 115.                liNode.appendChild(musicNode);   
  116. 116.                listNode.appendChild(liNode);   
  117. 117.                div = new musicDiv;   
  118. 118.                div.initNo(currentNo);   
  119. 119.                div.initEvent();   
  120. 120.                console.log(musicPath);   
  121. 121.            }   
  122. 122.        };   
  123. 123.           
  124. 124.        var notImplementException = function(){   
  125. 125.            alert('coding...,please wait for next version');   
  126. 126.        };   
  127. 127.      
  128. 128.      
  129. 129.        var btnAdd = document.getElementById('add');   
  130. 130.        btnAdd.onclick = addMusic;   
  131. 131.        var btnDel = document.getElementById('delete');   
  132. 132.        var btnPlay = document.getElementById('play');   
  133. 133.        var btnStop = document.getElementById('stop');   
  134. 134.        btnDel.onclick = notImplementException;   
  135. 135.        btnPlay.onclick = notImplementException;   
  136. 136.        btnStop.onclick = notImplementException;   
  137. 137.   })(window);   
  138. 138.   </script>  
  139. 139.</body>  
  140. 140.</html>  
  141. <!DOCTYPE html>
  142. <html>
  143. <head>
  144. <style type="text/css">
  145. button {
  146.         width: 80px;
  147.         height: 24px;
  148. }

  149. input[type="number"]{
  150.         width: 100px;
  151.         height: 24px;
  152. }

  153. ul{
  154.         list-style:none;
  155. }

  156. li{
  157.         list-style:none;
  158. }

  159. </style>
  160. </head>
  161. <body>
  162.         <div id="operations">
  163.                 <div>
  164.                         <button type="button" id="add" name="add">Add</button>
  165.                 </div>
  166.                 <div>
  167.                         <button type="button" id="delete" name="delete">Delete</button>
  168.                         <input type="number" placeholder="music num">
  169.                 </div>
  170.                 <div>
  171.                         <button type="button" id="play" name="play">Play</button>
  172.                 </div>
  173.                 <div>
  174.                         <button type="button" id="stop" name="stop">Stop</button>
  175.                 </div>
  176.    </div>
  177.    <div>
  178.                 <ul id="listNode">
  179.                 </ul>
  180.    </div>
  181.    <script type="text/javascript">
  182.    (function(window, undifined){
  183.    
  184.                 var musicDiv = function(){
  185.                        
  186.                         this._musicList = document.getElementsByTagName('audio');
  187.                        
  188.                         this._musicEndedListener = function(){
  189.                                 this.currentTime = 0;
  190.                                 this.pause();
  191.                                 var currentId = this.getAttribute("id");
  192.                                 var arrId = currentId.split('_');
  193.                                 var nextMusic = document.getElementById(arrId[0] + "_" + (parseInt(arrId[1]) + 1));
  194.                                 if(nextMusic){
  195.                                         nextMusic.play();
  196.                                 }else{
  197.                                         document.getElementById(arrId[0] + "_" + "1").play();
  198.                                 }
  199.                         };
  200.                        
  201.                 };
  202.                
  203.                 musicDiv.prototype = {
  204.                        
  205.                         initNo: function(startNo){
  206.                                 var numSpan = null;
  207.                                 var textNode = null;
  208.                                 var liNodes = document.getElementsByTagName('li');
  209.                                 for(var i = 1; i <= liNodes.length; i++){
  210.                                         numSpan = document.createElement('span');
  211.                                         textNode = document.createTextNode("No. " + i);
  212.                                         numSpan.appendChild(textNode);
  213.                                         if(!startNo){
  214.                                                 liNodes[i - 1].parentNode.insertBefore(numSpan, liNodes[i - 1]);
  215.                                         }else{
  216.                                                 if(i >= startNo){
  217.                                                         liNodes[i - 1].parentNode.insertBefore(numSpan, liNodes[i - 1]);
  218.                                                 }
  219.                                         }
  220.                                 }
  221.                         },
  222.                         initEvent: function(){
  223.                                 for(var i = 0; i < this._musicList.length; i++){
  224.                                         this._musicList[i].removeEventListener('ended', this._musicEndedListener, false);
  225.                                         this._musicList[i].addEventListener('ended',this._musicEndedListener);
  226.                                 }
  227.                         }
  228.                 };
  229.                
  230.                 var div = new musicDiv;
  231.                 div.initNo();
  232.                 div.initEvent();
  233.                
  234.                
  235.                 var addMusic = function(){
  236.                         var currentNo = document.getElementsByTagName('audio').length + 1;
  237.                         var musicNode = null;
  238.                         var sourceNode = null;
  239.                         var liNode = null;
  240.                         var listNode = document.getElementById('listNode');
  241.                         var musicPath = prompt("Please copy the full path of music(mp3 only) in your disk:");
  242.                         if(musicPath){
  243.                                 liNode = document.createElement('li');
  244.                                 musicNode = document.createElement('audio');
  245.                                 musicNode.setAttribute('id', 'music_' +  currentNo);
  246.                                 musicNode.setAttribute('controls', 'controls');
  247.                                 sourceNode = document.createElement('source');
  248.                                 sourceNode.setAttribute('src', musicPath);
  249.                                 sourceNode.setAttribute('type', 'audio/mpeg');
  250.                                 musicNode.appendChild(sourceNode);
  251.                                 liNode.appendChild(musicNode);
  252.                                 listNode.appendChild(liNode);
  253.                                 div = new musicDiv;
  254.                                 div.initNo(currentNo);
  255.                                 div.initEvent();
  256.                                 console.log(musicPath);
  257.                         }
  258.                 };
  259.                
  260.                 var notImplementException = function(){
  261.                         alert('coding...,please wait for next version');
  262.                 };
  263.    
  264.    
  265.                 var btnAdd = document.getElementById('add');
  266.                 btnAdd.onclick = addMusic;
  267.                 var btnDel = document.getElementById('delete');
  268.                 var btnPlay = document.getElementById('play');
  269.                 var btnStop = document.getElementById('stop');
  270.                 btnDel.onclick = notImplementException;
  271.                 btnPlay.onclick = notImplementException;
  272.                 btnStop.onclick = notImplementException;
  273.    })(window);
  274.    </script>
  275. </body>
  276. </html>
复制代码
应用说明:首先确保你已经安装了chrome,而且电脑上面有mp3格式的音乐。
进入页面之后点击add按钮,把mp3的全路径copy到对话框中,这样你的音乐就会出现在页面上了,点击一个播放,然后就会开始列表循环播放。
========================================分割线=========================
欢迎感兴趣的朋友一起开发,交流,共同提高。
效果图:

还有就是请大家帮有一下票:
地址:http://html5.group.iteye.com/group/topic/28355
回复:支持1楼
非常感谢大家的支持。

ab7b80f8-7198-315e-ae52-6fed79db068a.jpg (21.55 KB, 下载次数: 12)

ab7b80f8-7198-315e-ae52-6fed79db068a.jpg

论坛徽章:
0
2 [报告]
发表于 2011-12-24 21:26 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP