【问题标题】:how to create video player in html 5?如何在 html 5 中创建视频播放器?
【发布时间】:2015-10-02 16:48:10
【问题描述】:

我的视频播放器不工作,当我点击播放按钮时它不播放。我正在chrome浏览器上测试它。

这是我的代码(我认为问题出在 JS 部分):

function dofirst() {
  barSize = 500;
  video = document.getElementById('video');
  playbutton = document.getElementById('playbutton');
  defaultBar = document.getElementById('defaultBar');
  progressbar = document.getElementById('progressbar');

  playbutton.addEventListener(click, PlayOrPause ,false);
  defaultBar.addEventListener(click, clickedBar ,false);
}

function PlayOrPause() {

  If( !video.paused && !video.ended){
    video.pause();
    playbutton.innerHTML = 'play';
    window.clearInterval(updatebar);
  } else {
    video.play();
    playbutton.innerHTML = 'pause';
    updatebar = setInterval(update,500);
  }
}

function update(){
  if(!video.ended){
    var size= parseInt(video.currentTime*barsize/video.duration);
    progressbar.style.width = size +'px';
  } else {
    progressbar.style.width ='0px';
    playbutton.innerHTML = 'play';
    window.clearInterval(updatebar);
  }
}

function clickedBar(e) {
  If( !video.paused && !video.ended){
    var mouseX = e.pageX-bar.offsetLeft;
    var newtiem = mouseX*video.duration/barSize;
    myMovie.currentTime = newtime;
    progressbar.style.width = mouseX+'px';
  }
}

window.addEventListener('load',dofirst,false);
body {
  text-align: center;
}
#skin {
  background: #5C6366;
  width: 700px;
  margin: 10px auto;
  padding: 50px;
  border: 2px black auto;
  border-radius: 30px;
}
nav {
  margin: 2px 0px;
}
#buttons {
  float: left;
  width: 70px;
  height: 20px;
  margin-left: 20px;/* 90px total 610 remaining*/
}
#defaultBar {
  margin-top: 5px;
  position: relative;
  width: 500px;
  float : left;
  height: 5px;
  background-color: black;
}
#progressbar {
  position: absolute;
  width: 0px;
  height: 5px;
  background-color: white;
}
<section id="skin" >
  <video  width=640px height=360px id="video" >
    <source src="e:\dc\SampleVideo_1080x720_5mb.mp4" type="video/mp4"/>
  </video>
  <nav>
    <div id="buttons">
      <button type="button" id="playbutton">play</button>
    </div>
    <div id="defaultBar">
      <div id="progressbar"></div>
    </div>
    <div style="clear:both" ></div>
  </nav>
</section>

怎么了?我需要修复什么才能使其正常工作?

【问题讨论】:

  • 尽量减少您在此处发布的代码数量,并修正您的描述的语法和标点符号。

标签: javascript html html5-video


【解决方案1】:

有两个错误导致此方法不起作用:

  1. JavaScript 是一种区分大小写的语言,您没有为关键字 if 使用正确的语法(您在两个不同的地方写了 If):

    if( !video.paused && !video.ended){
    
  2. 与您在 addEventListener 的引号之间为 load 事件添加事件名称的方式相同,您需要为 click 事件执行相同的操作:

    playbutton.addEventListener("click", PlayOrPause ,false);
    defaultBar.addEventListener("click", clickedBar ,false);
    

一旦你解决了这两个问题,它就会起作用。这是带有更正的代码:

function dofirst() {
  barSize = 500;
  video = document.getElementById('video');
  playbutton = document.getElementById('playbutton');
  defaultBar = document.getElementById('defaultBar');
  progressbar = document.getElementById('progressbar');

  playbutton.addEventListener("click", PlayOrPause ,false);
  defaultBar.addEventListener("click", clickedBar ,false);
}

function PlayOrPause() {
  if( !video.paused && !video.ended){
    video.pause();
    playbutton.innerHTML = 'play';
    window.clearInterval(updatebar);
  } else {
    video.play();
    playbutton.innerHTML = 'pause';
    updatebar = setInterval(update,500);
  }
}

function update(){
  if(!video.ended){
    var size= parseInt(video.currentTime*barsize/video.duration);
    progressbar.style.width = size +'px';
  } else {
    progressbar.style.width ='0px';
    playbutton.innerHTML = 'play';
    window.clearInterval(updatebar);
  }
}

function clickedBar(e) {
  if( !video.paused && !video.ended){
    var mouseX = e.pageX-bar.offsetLeft;
    var newtiem = mouseX*video.duration/barSize;
    myMovie.currentTime = newtime;
    progressbar.style.width = mouseX+'px';
  }
}

window.addEventListener('load',dofirst,false);
body {
  text-align: center;
}
#skin {
  background: #5C6366;
  width: 700px;
  margin: 10px auto;
  padding: 50px;
  border: 2px black auto;
  border-radius: 30px;
}
nav {
  margin: 2px 0px;
}
#buttons {
  float: left;
  width: 70px;
  height: 20px;
  margin-left: 20px;/* 90px total 610 remaining*/
}
#defaultBar {
  margin-top: 5px;
  position: relative;
  width: 500px;
  float : left;
  height: 5px;
  background-color: black;
}
#progressbar {
  position: absolute;
  width: 0px;
  height: 5px;
  background-color: white;
}
<section id="skin" >
  <video  width=640px height=360px id="video" >
    <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"/>
  </video>
  <nav>
    <div id="buttons">
      <button type="button" id="playbutton">play</button>
    </div>
    <div id="defaultBar">
      <div id="progressbar"></div>
    </div>
    <div style="clear:both" ></div>
  </nav>
</section>

注意:我将视频路径更新为在线实际可用的路径,以表明视频在上述更改后可以正常工作。

【讨论】:

    猜你喜欢
    • 2013-06-02
    • 2011-03-02
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多