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 相关文章推荐
javascript引用对象的方法代码
Aug 13 Javascript
Javascript 继承实现例子
Aug 12 Javascript
js change,propertychange,input事件小议
Dec 20 Javascript
浅析Js中的单引号与双引号问题
Nov 06 Javascript
js判断横竖屏及禁止浏览器滑动条示例
Apr 29 Javascript
gameboy网页闯关游戏(riddle webgame)--仿微信聊天的前端页面设计和难点
Feb 21 Javascript
使用getBoundingClientRect方法实现简洁的sticky组件的方法
Mar 22 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
Apr 17 Javascript
浅谈JS正则表达式的RegExp对象和括号的使用
Jul 28 Javascript
js实现微博发布小功能
Jan 12 Javascript
JS实现图片高斯模糊切换效果的焦点图实例
Jan 21 Javascript
微信小程序 动态绑定事件并实现事件修改样式
Apr 13 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
星际争霸 Starcraft 游戏介绍
2020/03/14 星际争霸
一个简单的php加密解密函数(动态加密)
2013/06/19 PHP
php用header函数实现301跳转代码实例
2013/11/25 PHP
codeigniter教程之多文件上传使用示例
2014/02/11 PHP
codeigniter教程之上传视频并使用ffmpeg转flv示例
2014/02/13 PHP
PHP计算加权平均数的方法
2015/07/16 PHP
完美解决phpexcel导出到xls文件出现乱码的问题
2016/10/29 PHP
php监测数据是否成功插入到Mysql数据库的方法
2016/11/25 PHP
详解PHP文件的自动加载(autoloading)
2018/02/04 PHP
在Laravel中使用MongoDB的方法示例
2019/11/11 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
javascript背投广告代码的完善
2008/04/08 Javascript
JavaScript 函数参数是传值(byVal)还是传址(byRef) 分享
2013/07/02 Javascript
JS+CSS 制作的超级简单的下拉菜单附图
2013/11/22 Javascript
JS将光标聚焦在文本最后的实现代码
2014/03/28 Javascript
js 获取时间间隔实现代码
2014/05/12 Javascript
javascript常用正则表达式汇总
2015/07/31 Javascript
无循环 JavaScript(map、reduce、filter和find)
2017/04/08 Javascript
webpack中CommonsChunkPlugin详细教程(小结)
2017/11/09 Javascript
vue.js根据代码运行环境选择baseurl的方法
2018/02/28 Javascript
vue+elementUI动态生成面包屑导航教程
2019/11/04 Javascript
python 实现文件的递归拷贝实现代码
2012/08/02 Python
举例讲解Python的Tornado框架实现数据可视化的教程
2015/05/02 Python
Python基于time模块求程序运行时间的方法
2017/09/18 Python
Python操作MySQL数据库的三种方法总结
2018/01/30 Python
python通过tcp发送xml报文的方法
2018/12/28 Python
浅谈django channels 路由误导
2020/05/28 Python
通俗易懂了解Python装饰器原理
2020/09/17 Python
详解Java中一维、二维数组在内存中的结构
2021/02/11 Python
林肯就职演讲稿
2014/05/19 职场文书
幼儿园八一建军节活动方案
2014/08/27 职场文书
2015年销售助理工作总结
2015/05/11 职场文书
java基础——多线程
2021/07/03 Java/Android
Python3.10的一些新特性原理分析
2021/09/15 Python
Win11 BitLocker 驱动器加密
2022/04/19 数码科技