This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmideo.min.js
1 lines (1 loc) · 4.45 KB
/
mideo.min.js
1
function Cm_Player(b){this.container=b;this.duration=0;this.muted=false;this.currentPercentage=0;this.video=this.container.querySelector("video");this.playButton=this.container.querySelector(".cm_play");this.pauseButton=this.container.querySelector(".cm_pause");this.volumeButton=this.container.querySelector(".cm_vol");this.muteState=this.container.querySelector(".cm_mute");this.fwdButton=this.container.querySelector(".cm_fwd");this.rewButton=this.container.querySelector(".cm_rew");this.timeText=this.container.querySelector(".cm_time");this.timeRevText=this.container.querySelector(".cm_time_rev");this.caretPosition=this.container.querySelector(".cm_caret");this.caretWidth=this.caretPosition.offsetWidth;this.trailProgress=this.container.querySelector(".cm_trail");this.trailTotal=this.container.querySelector(".cm_trail_total");this.seeker=false;this.seeking=false;this.trailSeek=false;this.trailWidth=this.trailTotal.offsetWidth;this.triggerCanPlay=true;var a=this.video,c=this;findPos=function(d){var e=curtop=0;if(d.offsetParent){do{e+=d.offsetLeft;curtop+=d.offsetTop}while(d=d.offsetParent)}return[e,curtop]};this.offsetTrail=findPos(this.trailProgress)[0];seekWithTrail=function(d){if(c.trailSeek){c.seekToPercentage((d.pageX-c.offsetTrail)/c.trailWidth)}};this.trailProgress.addEventListener("mousedown",function(d){c.trailSeek=true;seekWithTrail(d)},false);this.trailProgress.addEventListener("mousemove",seekWithTrail,false);window.addEventListener("mouseup",function(){c.trailSeek=false},false);this.playButton.addEventListener("click",function(){a.play()},false);this.pauseButton.addEventListener("click",function(){c.triggerCanPlay=false;a.pause()},false);this.volumeButton.addEventListener("click",function(){if(c.muted){c.muteState.style.display="block"}else{c.muteState.style.display="none"}c.muted=!c.muted;a.muted=c.muted},false);seek=function(){switch(c.seeking){case"fwd":var d=a.currentTime+1;a.currentTime=d<a.duration?d:a.duration;break;case"rew":var d=a.currentTime-1;a.currentTime=d>0?d:0;break}};startSeeking=function(d){c.seeking=d;c.seeker=setInterval("seek()",100)};stopSeeking=function(d){c.seeking=false;clearInterval(c.seeker)};this.fwdButton.addEventListener("mousedown",function(){startSeeking("fwd")},false);this.rewButton.addEventListener("mousedown",function(){startSeeking("rew")},false);this.fwdButton.addEventListener("mouseup",stopSeeking,false);this.rewButton.addEventListener("mouseup",stopSeeking,false);this.video.addEventListener("progress",this,false);this.video.addEventListener("durationchange",this,false);this.video.addEventListener("timeupdate",this,false);this.video.addEventListener("pause",this,false);this.video.addEventListener("play",this,false);this.video.addEventListener("load",this,false);this.video.addEventListener("canplaythrough",this,false);this.updateState()}Cm_Player.prototype.handleEvent=function(a){switch(a.type){case"durationchange":this.duration=this.video.duration;break;case"timeupdate":this.currentPercentage=this.video.currentTime/this.video.duration;this.caretPosition.style.left=(this.currentPercentage*(this.trailWidth-this.caretWidth*2))+"px";this.timeText.textContent=this.formatTime(this.video.currentTime);this.timeRevText.textContent="-"+this.formatTime(this.video.duration-this.video.currentTime);if(this.video.currentTime>=this.video.duration){this.video.pause()}break;case"play":this.updateBar(a);this.triggerCanPlay=true;case"pause":this.updateState();break;case"load":case"progress":this.updateBar(a);break;case"canplaythrough":if(this.triggerCanPlay){this.video.play()}break}};Cm_Player.prototype.seekToPercentage=function(a){if(this.video.duration>0){this.video.currentTime=this.video.duration*a}};Cm_Player.prototype.updateState=function(){if(this.video.paused){this.playButton.style.display="block";this.pauseButton.style.display="none"}else{this.playButton.style.display="none";this.pauseButton.style.display="block"}};Cm_Player.prototype.formatTime=function(b){var a=[];a[0]=Math.floor(b/3600);a[1]=Math.floor((b%3600)/60);a[2]=Math.floor(b%60);for(i=0;i<3;i++){a[i]=a[i]<10?"0"+a[i]:a[i]}return a.join(":")};Cm_Player.prototype.updateBar=function(c){var a=0;if(c.lengthComputable){a=c.loaded/c.total}else{if(typeof this.video.buffered!="undefined"){a=(this.video.buffered.end(0)/this.video.duration)}}var b=(this.trailWidth-2)*a;this.trailProgress.style.width=b+"px"};var player;init=function(){var b=document.querySelectorAll(".cm_player");var a=[];for(i=0;i<b.length;i++){a.push(new Cm_Player(b[i]))}};window.addEventListener("load",init,false);