javascript实现简单的html5视频播放器


Posted in Javascript onMay 06, 2015

效果:

javascript实现简单的html5视频播放器

javascript实现简单的html5视频播放器

代码很简单

js

define("html5_video_player", [ '../avalon-min'], function(avalon) {
  function formatTime(seconds) {
    var seconds = Math.round(seconds);
    var minutes = Math.floor(seconds / 60);
    seconds = Math.floor(seconds % 60);
    minutes = (minutes >= 10) ? minutes : "0" + minutes;
    seconds = (seconds >= 10) ? seconds : "0" + seconds;
    return minutes + ":" + seconds;
  }
  var playing=false,mute=false,vol=50,fs=false,active=false,inactivityTimeout=timer=null;
  avalon.bind($('control_btn'),'click',function(){
    if(!playing){
      $('html5_video').play();
      $('control_btn').className='html5_video_pause_btn inline-block';
      playing=true;
    }else{
      $('html5_video').pause();
      $('control_btn').className='html5_video_play_btn inline-block';
      playing=false;
    }
  });
  avalon.bind($('video_bar'),'click',function(e){
    var a=(e.clientX-$('video_bar').offsetLeft)/$('video_bar').offsetWidth;
    $('html5_video').currentTime =a.toFixed(2)*$('html5_video').duration;
  });
  avalon.bind($('vol_bar'),'click',function(e){
    var a=(e.clientX-$('vol_bar').offsetLeft-8)/$('vol_bar').offsetWidth;
    vol=$('html5_video').volume =a;
    $('vol_value').style.width=a*100+'%';
  });
  avalon.bind($('mute_icon'),'click',function(){
    if(!mute){
      $('html5_video').volume=0;
      $('mute_icon').className='html5_video_mute1';
      mute=true;
    }else{
      $('html5_video').volume=vol;
      $('mute_icon').className='html5_video_mute';
      mute=false;
    }
  });
  avalon.bind($('html5_video'),'loadedmetadata',function(){
    $('html5_video_duration').innerHTML=formatTime($('html5_video').duration);
    $('html5_video').volume=0;
  });
  avalon.bind($('html5_video'),'timeupdate',function(){
    $('html5_play_time').innerHTML=formatTime($('html5_video').currentTime);
    $('video_progress_bar').style.left=$('html5_video').currentTime/$('html5_video').duration*100+'%';
  });
  avalon.bind($('html5_video_fullscreen'),'click',function(e){
    if(!fs){
      toggle_fullscreen();
    }else{
      exit_fullscreen();
    }
  });
  document.onmozfullscreenchange = function() {
    if ($('html5_video').clientWidth +2!= document.documentElement.clientWidth) {
      exit_fullscreen();
    }
  };
  document.onwebkitfullscreenchange = function() {
    if ($('html5_video').clientWidth!= document.documentElement.clientWidth) {
      exit_fullscreen();
    }
  };
  function exit_fullscreen() {
    $('html5_video').className='';
    fs = false;
    active=false;
    $('video_control').className='';
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    }
  }
  function toggle_fullscreen() {
    $('html5_video').className='video_fs';
    fs = true;
    $('video_control').className='fullscreen';
    var elem=$('html5_video');
    if (elem.msRequestFullscreen) {
      elem.msRequestFullscreen();
    } else if (elem.mozRequestFullScreen) {
      elem.mozRequestFullScreen();
    } else if (elem.webkitRequestFullscreen) {
      elem.webkitRequestFullscreen();
    }
  }
  function updateBuffered() {
     var v = $('html5_video');
     var r = v.buffered;
     if (r) {
      for (var i=0; i<r.length; i++) {
       var start = r.start(i);
       var end = r.end(i);
      }
      $('video_buffer_bar').style.width=end/$('html5_video').duration*100+'%';
     }
    }
  setInterval(updateBuffered,500);
  function b(){
    if(active){
      $('video_control').style.display='none';
      active=false;
      console.log(active);
    }
  }
  avalon.bind($('html5_video'),'mousemove',function(e){
    if(fs){
      clearTimeout(inactivityTimeout);
      active=true;
      $('video_control').style.display='block';
      inactivityTimeout = setTimeout(b, 5000);
    }
  });
});

html

<link type="text/css"
  href="http://localhost/twitter/css/html5_video_player.css"
  rel="stylesheet" />
<div id='wrap_html5_video'>
  <div id='html5_video_area'>
    <video id="html5_video" width="360" height="240">
      <source type=" video/mp4" src="http://localhost/twitter/videos/2.mp4"></source>
      <source type=" video/webm"
        src="http://localhost/twitter/videos/2.webm"></source>
    </video>
  </div>
  <div id='video_control'>
    <div id='video_bar'>
      <div id='video_buffer_bar'></div>
      <div id='video_progress_bar'></div>
    </div>
    <div id='play_control'>
      <ul>
        <li class='inline-block'><a
          class='html5_video_play_btn inline-block' id='control_btn'></a></li>
        <li class='inline-block'><a id='mute_icon'
          class='html5_video_mute'></a>
          <div id='vol_bar' class='inline-block'>
            <p id='vol_value'></p>
          </div></li>
        <li class='inline-block' id='html5_video_time'><span
          id='html5_play_time'>00:00</span><span>/</span><span
          id='html5_video_duration'>33:44</span></li>
        <li class='inline-block'><a id='html5_video_fullscreen'
          class='inline-block'></a></li>
      </ul>
    </div>
    <div id='a'></div>
  </div>
  <div id='buffered_log'></div>
</div>
<script type="text/javascript">
  require('html5/html5_video_player');
</script>

css

@CHARSET "UTF-8";

#wrap_html5_video {
  padding: 10px;
  width: 360px;
}

#vol_bar,#video_bar,#vol_value {
  height: 9px;
  background-color: #999999;
}

#vol_bar {
  width: 100px;
  cursor: pointer;
}

#vol_value {
  background-color: #179df7;
  width: 50%;
}

#html5_video {
  display: block;
  border: 1px solid #c0deed;
}

#video_buffer_bar {
  background-color: #179DF7;
  width: 0;
}

#video_progress_bar,#video_buffer_bar {
  position: absolute;
  height: 100%;
}

#video_progress_bar {
  background-color: #0066FF;
  width: 2px;
  left: 0;
}

.html5_video_pause_btn,.html5_video_play_btn {
  width: 40px;
  height: 40px;
  cursor: pointer;
}

.html5_video_play_btn {
  background: url("http://localhost/twitter/images/html5_video.jpg") 0 0
    no-repeat;
}

.html5_video_play_btn:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -41px
    0 no-repeat;
}

.html5_video_pause_btn {
  background: url("http://localhost/twitter/images/html5_video.jpg") 0
    -42px no-repeat;
}

.html5_video_pause_btn:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -41px
    -42px no-repeat;
}

#play_control a,#vol_bar {
  vertical-align: middle;
}

#html5_video_fullscreen {
  width: 25px;
  background: url("http://localhost/twitter/images/html5_video.jpg") 0
    -310px no-repeat;
  height: 18px;
}

#play_control #html5_video_time {
  font-size: 14px;
}

#play_control li,#play_control ul {
  font-size: 0;
}

#play_control li:last-child {
  position: absolute;
  right: 0;
}

.html5_video_mute1 {
  background: url("http://localhost/twitter/images/html5_video.jpg")
    no-repeat scroll -79px -170px rgba(0, 0, 0, 0);
}

.html5_video_mute {
  background: url("http://localhost/twitter/images/html5_video.jpg")
    no-repeat scroll 0 -170px rgba(0, 0, 0, 0);
}

#mute_icon {
  cursor: pointer;
  display: inline-block;
  height: 15px;
  width: 18px;
}

.html5_video_mute:hover {
  background: url("http://localhost/twitter/images/html5_video.jpg") -19px
    -170px no-repeat;
}

#play_control li {
  height: 40px;
  vertical-align: top;
  margin: 0 5px;
}

#play_control li:after {
  display: inline-block;
  width: 0;
  height: 100%;
  vertical-align: middle;
  content: '';
}

#play_control,#video_bar,#vol_bar {
  position: relative;
}

body .fullscreen {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  overflow: hidden;
  z-index: 2147483647;
  background-color: #fff;
}

video::-webkit-media-controls {
  display: none !important;
}
Javascript 相关文章推荐
IE8 中使用加速器(Activities)
May 14 Javascript
Javascript匿名函数的一种应用 代码封装
Jun 27 Javascript
使用jquery为table动态添加行的实现代码
Mar 30 Javascript
加载 Javascript 最佳实践
Oct 30 Javascript
js类式继承的具体实现方法
Dec 31 Javascript
JS实现静止元素自动移动示例
Apr 14 Javascript
详解原生JavaScript实现jQuery中AJAX处理的方法
May 10 Javascript
深入理解JS addLoadEvent函数
May 20 Javascript
Bootstrap零基础入门教程(三)
Jul 18 Javascript
使用JavaScript进行表单校验功能
Aug 01 Javascript
判断滚动条滑到底部触发事件(实例讲解)
Nov 15 Javascript
学习JS中的DOM节点以及操作
Apr 30 Javascript
js实现的四级左侧网站分类菜单实例
May 06 #Javascript
wangEditor编辑器失去焦点后仍然可以在原位置插入图片分析
May 06 #Javascript
完美实现仿QQ空间评论回复特效
May 06 #Javascript
jQuery实现鼠标单击网页文字后在文本框显示的方法
May 06 #Javascript
js实现点击按钮后给Div图层设置随机背景颜色的方法
May 06 #Javascript
JS实现CheckBox复选框全选全不选功能
May 06 #Javascript
javascript使用avalon绑定实现checkbox全选
May 06 #Javascript
You might like
PHP循环语句笔记(foreach,list)
2011/11/29 PHP
php简单实现发送带附件的邮件
2015/06/10 PHP
WampServer搭建php环境时遇到的问题汇总
2015/07/23 PHP
PHP用户注册邮件激活账户的实现代码
2017/05/31 PHP
使Ext的Template可以解析二层的json数据的方法
2007/12/22 Javascript
jquery解析json格式数据的方法(对象、字符串)
2015/11/24 Javascript
Bootstrap前端开发案例一
2016/06/17 Javascript
jQuery layui常用方法介绍
2016/07/25 Javascript
jquery过滤特殊字符',防sql注入的实现方法
2016/08/17 Javascript
微信小程序 input输入框控件详解及实例(多种示例)
2016/12/14 Javascript
Vue指令的钩子函数使用方法
2017/03/20 Javascript
微信小程序实现顶部选项卡(swiper)
2020/06/19 Javascript
pace.js和NProgress.js两个加载进度插件的一点小总结
2018/01/31 Javascript
vuex提交state&amp;&amp;实时监听state数据的改变方法
2018/09/16 Javascript
layer ui插件显示tips时,修改字体颜色的实现方法
2019/09/11 Javascript
[02:32]“虐狗”镜头慎点 2016国际邀请赛中国区预选赛现场玩家采访
2016/06/28 DOTA
python框架django基础指南
2016/09/08 Python
python装饰器实例大详解
2017/10/25 Python
利用python将json数据转换为csv格式的方法
2018/03/22 Python
使用EduBlock轻松学习Python编程
2018/10/08 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
pandas.read_csv参数详解(小结)
2019/06/21 Python
python数据挖掘需要学的内容
2019/06/23 Python
pytorch 输出中间层特征的实例
2019/08/17 Python
python常用排序算法的实现代码
2019/11/08 Python
详解python中groupby函数通俗易懂
2020/05/14 Python
详解HTML5中的manifest缓存使用
2015/09/09 HTML / CSS
环法自行车赛官方商店:Le Tour de France
2017/08/27 全球购物
英国版MAC彩妆品牌:Illamasqua
2018/04/18 全球购物
女孩每月服装订阅盒:kidpik
2019/04/17 全球购物
高三励志标语
2014/06/05 职场文书
入党团支部推荐意见
2015/06/02 职场文书
2019请假条的基本格式及范文!
2019/07/05 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python
PHP正则表达式之RCEService回溯
2022/04/11 PHP