·您当前的位置:主页 > 技术教程 > Html5技术 >

[html5]html5视频播放器skin界面制作的源代码例子

时间:2017-09-25 18:21酷播
[html5]html5视频播放器skin界面制作的源代码例子

[html5]html5视频播放器skin界面制作的源代码例子

JS代码:

  1. // 为了不随意的创建全局变量,我们将我们的代码放在一个自己调用自己的匿名函数中,这是一个好的编程习惯 
  2.         (function(window, document){ 
  3.             // 获取要操作的元素 
  4.             var video = document.getElementById("video"); 
  5.             var videoControls = document.getElementById("videoControls"); 
  6.             var videoContainer = document.getElementById("videoContainer"); 
  7.             var controls = document.getElementById("video_controls"); 
  8.             var playBtn = document.getElementById("playBtn"); 
  9.             var fullScreenBtn = document.getElementById("fullScreenBtn"); 
  10.             var progressWrap = document.getElementById("progressWrap"); 
  11.             var playProgress = document.getElementById("playProgress"); 
  12.             var fullScreenFlag = false
  13.             var progressFlag; 
  14.  
  15.             // 创建我们的操作对象,我们的所有操作都在这个对象上。 
  16.             var videoPlayer = { 
  17.                 init: function(){ 
  18.                     var that = this
  19.                     video.removeAttribute("controls"); 
  20.                     bindEvent(video, "loadeddata", videoPlayer.initControls); 
  21.                     videoPlayer.operateControls(); 
  22.                 }, 
  23.                 initControls: function(){ 
  24.                     videoPlayer.showHideControls(); 
  25.                 }, 
  26.                 showHideControls: function(){ 
  27.                     bindEvent(video, "mouseover", showControls); 
  28.                     bindEvent(videoControls, "mouseover", showControls); 
  29.                     bindEvent(video, "mouseout", hideControls); 
  30.                     bindEvent(videoControls, "mouseout", hideControls); 
  31.                 }, 
  32.                 operateControls: function(){ 
  33.                     bindEvent(playBtn, "click", play); 
  34.                     bindEvent(video, "click", play); 
  35.                     bindEvent(fullScreenBtn, "click", fullScreen); 
  36.                     bindEvent(progressWrap, "mousedown", videoSeek); 
  37.                 } 
  38.             } 
  39.  
  40.             videoPlayer.init(); 
  41.  
  42.             // 原生的JavaScript事件绑定函数 
  43.             function bindEvent(ele, eventName, func){ 
  44.                 if(window.addEventListener){ 
  45.                     ele.addEventListener(eventName, func); 
  46.                 } 
  47.                 else{ 
  48.                     ele.attachEvent('on' + eventName, func); 
  49.                 } 
  50.             } 
  51.             // 显示video的控制面板 
  52.             function showControls(){ 
  53.                 videoControls.style.opacity = 1
  54.             } 
  55.             // 隐藏video的控制面板 
  56.             function hideControls(){ 
  57.                 // 为了让控制面板一直出现,我把videoControls.style.opacity的值改为1 
  58.                 videoControls.style.opacity = 1
  59.             } 
  60.             // 控制video的播放 
  61.             function play(){ 
  62.                 if ( video.paused || video.ended ){               
  63.                     if ( video.ended ){  
  64.                         video.currentTime = 0
  65.                         }  
  66.                     video.play(); 
  67.                     playBtn.innerHTML = "暂停";  
  68.                     progressFlag = setInterval(getProgress, 60); 
  69.                 }  
  70.                 else{  
  71.                     video.pause();  
  72.                     playBtn.innerHTML = "播放"
  73.                     clearInterval(progressFlag); 
  74.                 }  
  75.             } 
  76.             // 控制video是否全屏,额这一部分没有实现好,以后有空我会接着研究一下 
  77.             function fullScreen(){ 
  78.                 if(fullScreenFlag){ 
  79.                     videoContainer.webkitCancelFullScreen(); 
  80.                 } 
  81.                 else{ 
  82.                     videoContainer.webkitRequestFullscreen(); 
  83.                 } 
  84.             } 
  85.             // video的播放条 
  86.             function getProgress(){ 
  87.                 var percent = video.currentTime / video.duration; 
  88.                 playProgress.style.width = percent * (progressWrap.offsetWidth) - 2 + "px"; 
  89.                 showProgress.innerHTML = (percent * 100).toFixed(1) + "%"; 
  90.             } 
  91.             // 鼠标在播放条上点击时进行捕获并进行处理 
  92.             function videoSeek(e){ 
  93.                 if(video.paused || video.ended){ 
  94.                     play(); 
  95.                     enhanceVideoSeek(e); 
  96.                 } 
  97.                 else{ 
  98.                     enhanceVideoSeek(e); 
  99.                 } 
  100.  
  101.             } 
  102.             function enhanceVideoSeek(e){ 
  103.                 clearInterval(progressFlag); 
  104.                 var length = e.pageX - progressWrap.offsetLeft; 
  105.                 var percent = length / progressWrap.offsetWidth; 
  106.                 playProgress.style.width = percent * (progressWrap.offsetWidth) - 2 + "px"; 
  107.                 video.currentTime = percent * video.duration; 
  108.                 progressFlag = setInterval(getProgress, 60); 
  109.             } 
  110.  
  111.         }(this, document)) 

html代码:

  1. <!DOCTYPE html> 
  2. <html lang="en"> 
  3. <head> 
  4.     <meta charset="UTF-8"> 
  5.     <title>HTML5-Video-Player</title> 
  6.     <style type="text/css"> 
  7.         .videoPlayer{ 
  8.             border: 1px solid #000; 
  9.             width: 600px; 
  10.         } 
  11.         #video{ 
  12.             margin-top: 0px; 
  13.         } 
  14.         #videoControls{ 
  15.             width: 600px; 
  16.             margin-top: 0px; 
  17.         } 
  18.         .show{ 
  19.             opacity: 1; 
  20.         } 
  21.         .hide{ 
  22.             opacity: 0; 
  23.         } 
  24.         #progressWrap{ 
  25.             background-color: black; 
  26.             height: 25px; 
  27.             cursor: pointer; 
  28.         } 
  29.         #playProgress{ 
  30.             background-color: red; 
  31.             width: 0px; 
  32.             height: 25px; 
  33.             border-right: 2px solid blue; 
  34.         } 
  35.         #showProgress{ 
  36.             background-color: ; 
  37.             font-weight: 600; 
  38.             font-size: 20px; 
  39.             line-height: 25px; 
  40.         } 
  41.     </style> 
  42. </head> 
  43. <body> 
  44.     <div class=""> 
  45.         <h1>HTML5_Video_Player</h1> 
  46.         <div class="videoPlayer" id="videoContainer"> 
  47.             <video id="video"  
  48.             width="600" height="360"  
  49.             preload controls 
  50.             > 
  51.                 <source src="http://nettuts.s3.amazonaws.com/763_sammyJSIntro/trailer_test.mp4" type='video/mp4'> 
  52.                 <source src="http://nettuts.s3.amazonaws.com/763_sammyJSIntro/trailer_test.ogg" type='video/ogg'> 
  53.             </video> 
  54.             <div id="videoControls">  
  55.                 <div id="progressWrap">   
  56.                     <div id="playProgress">   
  57.                         <span id="showProgress">0</span>  
  58.                     </div> 
  59.                 </div> 
  60.                 <div> 
  61.                     <button id="playBtn" title="Play"> 播放 </button>  
  62.                     <button id="fullScreenBtn" title="FullScreen Toggle">  全屏 </button> 
  63.                 </div>  
  64.             </div>  
  65.         </div> 
  66.     </div> 
  67. </body> 
  68. </html> 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="pragma" content="no-cache"> 
  5. <meta name="wap-font-scale" content="no"> 
  6. <meta name="format-detection" content="telephone=no"/> 
  7. <meta name="apple-mobile-web-app-capable" content="yes"> 
  8. <meta name="apple-mobile-web-app-status-bar-style" content="blank"> 
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  10. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> 
  11. <script type="text/javascript" src="http://m.the3ctv.com/wap/jquery/jquery-1.8.3.min.js"></script> 
  12. <link type="text/css" rel="stylesheet" href="http://m.the3ctv.com/wap/css/common.css"> 
  13. <title>播放</title> 
  14. <style type="text/css">/*控制条的样式*/ 
  15. .video_play_main_div{background-color:#000000;width:100%;height:100%;} 
  16. .video_play_main_div .header{height:45px;} 
  17. .video_play_main_div .header a:nth-child(1){display: block;float: left;} 
  18. .video_play_main_div .header a:nth-child(1) img{height: 20px;margin-top: 12.5px;margin-left: 12.5px;} 
  19. .video_play_main_div .header a:nth-child(2){display: block;float: right;} 
  20. .video_play_main_div .header a:nth-child(2) img{height: 35px;margin-top: 5px;margin-right: 7px;} 
  21. .video_play_main_div .play_video{width:100%;position:absolute;top:50%;} 
  22. .video_play_main_div .play_video .preview{display:none;top: 50%;position: relative;height: 20px;text-align: center;background-color: white;border-radius: 20px;width: auto;} 
  23. .video_play_main_div .replay_name{color: white;position: absolute;bottom: 0px;margin-bottom: 20%;width: 100%;text-align: center;} 
  24. .the3ctv_video_control{width:100%;position:absolute;top: 61%;z-index: 3;background-color: black;height: 32px;} 
  25. .the3ctv_video_control a:nth-child(1){position: relative;float: left;} 
  26. .the3ctv_video_control a:nth-child(1) img{width: 25px;margin-left: 10px;} 
  27. .the3ctv_video_control em{float:left;color:white;margin-top:4px;} 
  28. .the3ctv_video_control em i:nth-child(1){} 
  29. .the3ctv_video_control em i:nth-child(2){color:#b4b4b4;} 
  30. .the3ctv_video_control span{display: block;height: 4px;width: 52%;margin-left: 2%;background-color: #505050;border-radius: 4px;margin-top: 11px;float:left;} 
  31. .the3ctv_video_control span b:nth-child(1){z-index:1;float:left;position:relative;width: 0%;background-color: #b4b4b4;display: block;height: 100%;border-radius: 4px;} 
  32. .the3ctv_video_control span b:nth-child(2){z-index:2;position:relative;float: left;width: 8px;height: 8px;background-color: white;border-radius: 8px;margin-top: -2px;margin-left: -8px;} 
  33. .the3ctv_video_control span b:nth-child(3){position: relative;background-color: white;display: block;height: 4px;border-radius: 4px;} 
  34. .the3ctv_video_control u{float:left;} 
  35. .the3ctv_video_control u img{display: block;width: 25px;margin-left: 5px;} 
  36. .trump-control-bottom,.trump-control-bottom-flow{display:none;} 
  37.  
  38. video::-webkit-media-controls-enclosure { 
  39.     /*禁用播放器控制栏的样式*/ 
  40.     /*display: none !important;*/ 
  41.  
  42. </style> 
  43. <script type="text/javascript"> 
  44. var flag = false
  45.      var initProgressBar = function(){        //进度条相关操作 
  46.          var main_div = $("#wap_video_play_main_div"); 
  47.          var video = $("video",main_div); 
  48.          var timeDrag = false;   /* Drag status */ 
  49.          $("span[name='progress']",main_div).mousedown(function(e) {     //进度条的按下操作 
  50.             timeDrag = true;  
  51.             updatebar(e.pageX); 
  52.          }); 
  53.          $(document).mouseup(function(e) {               //松开 
  54.             if(timeDrag) { 
  55.                timeDrag = false
  56.                updatebar(e.pageX); 
  57.             } 
  58.          }); 
  59.          $(document).mousemove(function(e) {  //鼠标移动事件 
  60.             if(timeDrag) { 
  61.                updatebar(e.pageX); 
  62.             } 
  63.          }); 
  64.  
  65.          //update Progress Bar control 
  66.          var updatebar = function(x) {  //更新时间与播放条进度 
  67.             var progress = $("span[name='progress']",main_div); 
  68.             var maxduration = video[0].duration; //Video duraiton 
  69.             var position = x - progress.offset().left; //Click pos 
  70.             var percentage = 100 * position / progress.width(); 
  71.             //Check within range 
  72.             if(percentage > 100) { 
  73.                percentage = 100
  74.             } 
  75.             if(percentage < 0) { 
  76.                percentage = 0
  77.             } 
  78.             //Update progress bar and video currenttime 
  79.             $("span b:eq(0)",main_div).css('width', percentage+'%'); 
  80.             video[0].currentTime = maxduration * percentage / 100; 
  81.             if(percentage==100){ 
  82.                 $("a[name='pause'] img",main_div).attr("src","F://pause.png") 
  83.             } 
  84.          }; 
  85.          $('u img',main_div).unbind().bind('click', function() {             //控制视频全屏与退出全屏 
  86.            //For Webkit 
  87.            video[0].webkitEnterFullscreen(); 
  88.  
  89.            //For Firefox 
  90.            video[0].mozRequestFullScreen(); 
  91.            video[0].controls=false
  92.            return false; 
  93.         }); 
  94.      } 
  95.     var initTimeLength = function(timeLength){             //根据秒数格式化时间 
  96.         timeLength = parseInt(timeLength); 
  97.         var second = timeLength%60; 
  98.         var minute = (timeLength-second)/60; 
  99.         return (minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second); 
  100.     } 
  101.     var initVideo = function(player){ 
  102.             flag = true
  103.             var main_div = $("#wap_video_play_main_div"); 
  104.         main_div.on("click","a[name='pause']",function(){    //暂停   继续 
  105.                 var video = $("video",main_div); 
  106.                 video.css("top","0px") 
  107.                 video.css("top","50%") 
  108.                 if(video[0].paused) { 
  109.                     video[0].play(); 
  110.             $("img",$(this)).attr("src","F://playing.png") 
  111.                 }else { 
  112.                      video[0].pause(); 
  113.                      $("img",$(this)).attr("src","F://pause.png") 
  114.                 } 
  115.                 return false; 
  116.             }); 
  117.             $("video",main_div).on('loadedmetadata', function() {       //获取全部视频长度 
  118.                 var video = $("video",main_div); 
  119.                $("i:eq(1)",main_div).html(initTimeLength(video[0].duration)); 
  120.             }); 
  121.             $("video",main_div).on('timeupdate', function() {           //实时更新播放进度条和时间 
  122.                 var video = $("video",main_div); 
  123.                 var currentPos = video[0].currentTime; //Get currenttime    //当前时间 
  124.                 var maxduration = video[0].duration; //Get video duration   //总时间 
  125.                 var percentage = 100 * currentPos / maxduration; //in % 
  126.                 $("i:eq(0)",main_div).html(initTimeLength(video[0].currentTime)); 
  127.                 $("span b:eq(0)",main_div).css("width",percentage+"%") 
  128.                 $("i:eq(1)",main_div).html(initTimeLength(video[0].duration)); 
  129.                 if(currentPos==maxduration){ 
  130.                     $("a[name='pause'] img",main_div).attr("src","F://pause.png") 
  131.                 } 
  132.             }); 
  133.             //$("video",main_div)[0].controls=false
  134.             //$("video",main_div).removeAttr("controls"); 
  135.             //$("video",main_div).attr("controls",null);           //隐藏控制条 
  136.             var startBuffer = function() {                       //预加载视频的 
  137.                 var video = $("video",main_div); 
  138.                var maxduration = video[0].duration; 
  139.                var currentBuffer = video[0].buffered.end(0); 
  140.                var percentage = 100 * currentBuffer / maxduration; 
  141.                $("span b:eq(2)").css('width', percentage+'%'); 
  142.  
  143.                if(currentBuffer < maxduration) { 
  144.                   setTimeout(startBuffer, 500); 
  145.                } 
  146.             }; 
  147. //          setTimeout(startBuffer, 500); 
  148.             initProgressBar(); 
  149.     } 
  150.     var setControl = function(){ 
  151.         initVideo(); 
  152. //      $("#wap_video_play_main_div video").removeAttr("controls"); 
  153. //      $(".trump-control-bottom,.trump-control-bottom-flow").hide(); 
  154. //      $("#trump_main_unique_1 div[data-resize-module='bottom']").closest("div").hide(); 
  155.     } 
  156.     $(function(){ 
  157.         var main_div = $("#wap_video_play_main_div"); 
  158.         main_div.height($(window).height()); 
  159.         var height = ($(window).width()/16)*9; 
  160.  
  161.         $("video",main_div).height(height).css("margin-top",-(height/2)) 
  162.  
  163.  
  164.         setControl() 
  165.  
  166.     }); 
  167. </script> 
  168. </head> 
  169. <body> 
  170.     <div class="video_play_main_div" id="wap_video_play_main_div"> 
  171.  
  172.         <video controls="controls" autobuffer oncontextmenu="return false;" preload="none" x-webkit-airplay="true" webkit-playsinline="true" id="video_id_1464329192793" width="100%" style="z-index: 1; overflow: hidden; box-sizing: border-box; position: absolute; left: 0px; top: 50%;"> 
  173.             <source src="http://200004500.vod.myqcloud.com/200004500_44e60aba1da111e689690f47f5e05cec.f30.mp4" type="video/mp4"> 
  174.         </video> 
  175.         <div class="the3ctv_video_control"> 
  176.             <a name="pause"> 
  177.                 <img src="F://pause.png"/> 
  178.             </a> 
  179.             <em><i name="time">00:00</i>&nbsp;|&nbsp;<i>01:21</i></em> 
  180.             <span name="progress"> 
  181.                 <b></b> 
  182.                 <b></b> 
  183.                 <b></b> 
  184.             </span> 
  185.             <u><img alt="" src="http://m.the3ctv.com/wap/images/video/fullScreen.png"/></u> 
  186.         </div>  
  187.     </div> 
  188. </body> 
  189. </html> 

来源: http://blog.csdn.net/ishangxinyu/article/details/51882518

微信付费直播
酷播云免费在线视频整体解决方案
参与开学季云视频优惠,拿GET2017教育科技大会VIP门票
进入开学季,保利威视推出云视频优惠,云视频套餐、流量超实惠大放“价”就是现在,还可以免费获取GET2017教育科技大会VIP门票哦!