Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Video custom additional seek bar

I am tinkering around with HTML5 videos. I have a video working by using the vanilla HTML5 <video> tag, something like this:

<video id="video" width="250" height="250" controls>
    <source src="video_src.mp4" type="video/mp4">
</video>

All is well. What I'm looking for is a way to have an additional seek bar at the bottom of the video. The seekbar will be an image I have that represents the video. By clicking anywhere on the image, the video will move to that point.

Again, this will work in addition to the default progress bar that comes with the default video functionality. The default and the custom seekbar would have to be in sync so that when one is updated, the other moves as well.

Can anyone point me to the right direction?

Thanks!

like image 661
intl Avatar asked Jan 31 '17 09:01

intl


People also ask

What is the HTML 5 element for adding video to a Web page?

The HTML <video> element is used to embed video in web documents. It may contain one or more video sources, represented using the src attribute or the source element. The <video> element is supported by all modern browsers.


1 Answers

var vid = document.getElementById("video");
vid.ontimeupdate = function(){
  var percentage = ( vid.currentTime / vid.duration ) * 100;
  $("#custom-seekbar span").css("width", percentage+"%");
};

$("#custom-seekbar").on("click", function(e){
    var offset = $(this).offset();
    var left = (e.pageX - offset.left);
    var totalWidth = $("#custom-seekbar").width();
    var percentage = ( left / totalWidth );
    var vidTime = vid.duration * percentage;
    vid.currentTime = vidTime;
});//click()
#custom-seekbar
{  
  cursor: pointer;
  height: 10px;
  margin-bottom: 10px;
  outline: thin solid orange;
  overflow: hidden;
  position: relative;
  width: 400px;
}
#custom-seekbar span
{
  background-color: orange;
  position: absolute;
  top: 0;
  left: 0;
  height: 10px;
  width: 0px;
}

/* following rule is for hiding Stack Overflow's console  */
.as-console-wrapper{ display: none !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="custom-seekbar">
  <span></span>
</div>
<video id="video" width="400" controls autoplay>
    <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
like image 79
Mohit Bhardwaj Avatar answered Oct 06 '22 00:10

Mohit Bhardwaj