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

[html5]html5倍速播放功能源代码实例

时间:2019-03-21 11:28csdn.net
[html5]html5倍速播放功能源代码实例,多倍速观看效果

[html5]html5倍速播放功能源代码实例

  1. var myVideo = document.getElementById("video");//播放器 
  2. var coolPlay = document.getElementById("cool-play"); 
  3. var cPlay = document.getElementById("c-play");//播放按钮 
  4. var cProgress = document.getElementById("c-progress"); 
  5. var cPlayed = document.getElementById("c-played");//已经播放过的进度条 
  6. var cDrag = document.getElementById("c-drag");//进度条顶端的圆钮 
  7. var cCurrentTime = document.getElementById("c-currentTime");//当前时间span 
  8. var cTotalTime = document.getElementById("c-totalTime");//总时间 
  9. var loading = document.getElementsByClassName("icon-c-loading");//loading 旋转图标 
  10. var refresh = document.getElementsByClassName("icon-c-refresh");//重新加载按钮 
  11. var voice = document.getElementsByClassName("i-voice");//音量按钮 
  12. var voice_mask = document.getElementsByClassName("voice-mask");//音量总进度条 
  13. var voice_bared= document.getElementsByClassName("voice-bared");//现在的音量的进度条 
  14. var voice_dot = document.getElementsByClassName("voice-dot"); 
  15. var voice_num = 0.8;//初始化当前的音量 
  16. volume(voice_num);//把音量初始化到80 
  17. function volume(n){ 
  18.   myVideo.volume = n; 
  19.   voice_bared[0].style.height=n*100 + 'px'; 
  20. } 
  21. function playPause() { 
  22.   if(myVideo.paused) { 
  23.     Play(); 
  24.   } else { 
  25.     Pause(); 
  26.   } 
  27. }; 
  28. function Play(){ 
  29.   cPlay.className = "icon-c-pause"; 
  30.   myVideo.play(); 
  31. } 
  32. function Pause(){ 
  33.   cPlay.className = "icon-c-play"; 
  34.   myVideo.pause(); 
  35. } 
  36. refresh[0].onclick = function (){ 
  37.   Load(); 
  38. } 
  39. function Load(){ 
  40.   Pause(); 
  41.   myVideo.load(); 
  42.   cPlayed.style.width = 0+"%"; 
  43.   cDrag.style.display="none"; 
  44.   cCurrentTime.innerHTML = "00:00"; 
  45.   cTotalTime.innerHTML = "00:00"; 
  46. } 
  47. //播放时间及进度条控制 
  48. myVideo.ontimeupdate = function(){   
  49.   var currTime = this.currentTime,    //当前播放时间 
  50.       duration = this.duration;       // 视频总时长 
  51.   if(currTime == duration){ 
  52.     Pause(); 
  53.   } 
  54.   //百分比 
  55.   var pre = currTime / duration * 100 + "%"; 
  56.   //显示进度条 
  57.   cPlayed.style.width = pre; 
  58.   var progressWidth = cProgress.offsetWidth; 
  59.   var leftWidth = progressWidth*(currTime / duration); 
  60.   if(leftWidth > 8 && (progressWidth - leftWidth) > 4){ 
  61.     cDrag.style.display="block";     
  62.   } else { 
  63.     cDrag.style.display="none"; 
  64.   }   
  65.   cDrag.style.left = progressWidth*(currTime / duration)-4 + "px"; 
  66.   //显示当前播放进度时间 
  67.   cCurrentTime.innerHTML = getFormatTime(currTime,duration); 
  68.   cTotalTime.innerHTML = getFormatTime(duration,duration); 
  69. }; 
  70. //当浏览器可在不因缓冲而停顿的情况下进行播放时 
  71. myVideo.oncanplaythrough = function() { 
  72.   loading[0].style.display="none"; 
  73. } 
  74.   //当视频由于需要缓冲下一帧而停止 
  75. myVideo.onwaiting = function() { 
  76.   loading[0].style.display="block"; 
  77. } 
  78. //当用户开始移动/跳跃到音频/视频中的新位置时 
  79. myVideo.onseeking = function() { 
  80.   if (myVideo.readyState == 0 || myVideo.readyState == 1) { 
  81.     loading[0].style.display="block"; 
  82.   } 
  83. } 
  84. //拖拽进度条上的园钮实现跳跃播放 
  85. /*cDrag.onmousedown = function(e){  
  86.   if(cPlay.className == 'icon-c-pause') 
  87.     myVideo.pause(); 
  88.   loading[0].style.display="block"; 
  89.   document.onmousemove = function(e){ 
  90.     console.log("e.clientX:"+e.clientX); 
  91.     console.log("coolPlay.offsetLeft:"+coolPlay.offsetLeft); 
  92.  
  93.     var leftV = e.clientX - coolPlay.offsetLeft; 
  94.     console.log("coolPlay.offsetLeft:"+coolPlay.offsetLeft); 
  95.     console.log("leftV:"+leftV); 
  96.     if(leftV <= 0){ 
  97.       leftV = 0; 
  98.     } 
  99.     if(leftV >= coolPlay.offsetWidth){ 
  100.       leftV = coolPlay.offsetWidth-10; 
  101.     } 
  102.     cDrag.style.left = leftV+"px"; 
  103. //  console.log(leftV); 
  104.   }     
  105.   document.onmouseup = function (){ 
  106.     var scales = cDrag.offsetLeft/cProgress.offsetWidth; 
  107.     var du = myVideo.duration*scales; 
  108.     console.log("scales:"+scales); 
  109.     console.log("du:"+du); 
  110.     myVideo.currentTime = du; 
  111.     if(cPlay.className == 'icon-c-pause') 
  112.       myVideo.play();      
  113.     document.onmousemove = null; 
  114.     document.onmousedown = null; 
  115.   } 
  116. }*/ 
  117. //在进度条上点击跳跃播放 
  118. cProgress.onclick = function(e){ 
  119.   var eevent = e || window.event; 
  120.   console.log("当前点击的位置为:"+(event.offsetX / this.offsetWidth) * myVideo.duration); 
  121.   myVideo.currentTime = (event.offsetX / this.offsetWidth) * myVideo.duration; 
  122. }; 
  123. //根据duration总长度返回 00 或 00:00 或 00:00:00 三种格式 
  124. function getFormatTime(time,duration) { 
  125.   var timetime = time || 0;          
  126.  
  127.   var h = parseInt(time/3600), 
  128.       m = parseInt(time%3600/60), 
  129.       s = parseInt(time%60); 
  130.   ss = s < 10 ? "0"+s : s; 
  131.   if(duration>=60 && duration <3600){ 
  132.     mm = m < 10 ? "0"+m : m;  
  133.     return m+":"+s; 
  134.   } 
  135.   if (duration >=3600){ 
  136.     mm = m < 10 ? "0"+m : m;  
  137.     hh = h < 10 ? "0"+h : h; 
  138.     return h+":"+m+":"+s; 
  139.   }  
  140.   return s; 
  141. }    
  142. //音量的控制部分 
  143. //点击声音按钮时,把视频静音 
  144. voice[0].onclick = function(){   
  145.   if(myVideo.muted){ 
  146.     voice[0].className="i-voice icon-c-voice"; 
  147.     myVideo.muted=false; 
  148.     if(voice_num >= 0 && voice_num <= 1){ 
  149.       volume(voice_num); 
  150.     } else { 
  151.       volume(0.8); 
  152.     }     
  153.   } else { 
  154.     voice_num = myVideo.volume; //记录下来静音前的音量 
  155.     voice[0].className='i-voice icon-c-mute'; 
  156.     myVideo.muted=true; 
  157.     volume(0); 
  158.   }   
  159. } 
  160. //当点击进度条上的一个位置时,变化音量 
  161. voice_mask[0].onclick = function(e){ 
  162.   var eevent = e || window.event;   
  163.   if(event.offsetY >= 100){ 
  164.     voice[0].className='i-voice icon-c-mute'; 
  165.     myVideo.muted=true; 
  166.     volume(0); 
  167.     return; 
  168.   } 
  169.   volume((100-event.offsetY)/100); 
  170. }; 
  171. /*voice_mask[0].onmousedown = function(e){  
  172.   document.onmousemove = function(e){ 
  173.     console.log("e.clientY:"+e.clientY); 
  174.     console.log("e.offsetY:"+e.offsetY); 
  175.     console.log(e); 
  176.     console.log(voice[0].offsetTop); 
  177.     console.log(document.getElementsByClassName("voice")[0]); 
  178.     volume((100-e.offsetY)/100); 
  179.     if(event.offsetY == 100){ 
  180.       voice[0].className='i-voice icon-c-mute'; 
  181.       myVideo.muted=true; 
  182.       volume(0); 
  183.     }    
  184.   }     
  185.   document.onmouseup = function (){     
  186.     document.onmousemove = null; 
  187.     document.onmousedown = null; 
  188.   } 
  189. }*/ 
  190. //全屏的控制部分 
  191. var fullscreen = document.getElementById('cool-fullScreen'); 
  192. var FullScreenTF = true; 
  193. function launchFullscreen(element) { 
  194.   //此方法不能在异步中进行,否则火狐中不能全屏操作 
  195.   if(element.requestFullscreen) { 
  196.     element.requestFullscreen(); 
  197.   } else if(element.mozRequestFullScreen) { 
  198.     element.mozRequestFullScreen(); 
  199.   } else if(element.msRequestFullscreen) { 
  200.     element.msRequestFullscreen(); 
  201.   } else if(element.oRequestFullscreen) { 
  202.     element.oRequestFullscreen(); 
  203.   } else if(element.webkitRequestFullscreen) { 
  204.     element.webkitRequestFullScreen(); 
  205.   } else { 
  206.     alert("您的浏览器版本太低,不支持全屏功能!"); 
  207.   } 
  208.   FullScreenTF = false; 
  209. }; 
  210. //退出全屏 
  211. function exitFullscreen() { 
  212.   if(document.exitFullscreen) { 
  213.     document.exitFullscreen(); 
  214.   } else if(document.msExitFullscreen) { 
  215.     document.msExitFullscreen(); 
  216.   } else if(document.mozCancelFullScreen) { 
  217.     document.mozCancelFullScreen(); 
  218.   } else if(document.oRequestFullscreen) { 
  219.     document.oCancelFullScreen(); 
  220.   } else if(document.webkitExitFullscreen) { 
  221.     document.webkitExitFullscreen(); 
  222.   } else { 
  223.     alert("您的浏览器版本太低,不支持全屏功能!"); 
  224.   } 
  225.   FullScreenTF = true; 
  226. }; 
  227. fullscreen.onclick = function() {        
  228.   if(FullScreenTF) { 
  229.     launchFullscreen(coolPlay); 
  230.     fullscreen.className = "icon-c-shrink";          
  231.   } else { 
  232.     exitFullscreen(); 
  233.     fullscreen.className = "icon-c-enlarge";           
  234.   } 
  235. }; 

来源:https://blog.csdn.net/yaozhiqi1905658804

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器