control slider and video

An example of controlling both a slider and video time responsively

by tehsurfer

HTML

Frame number:
<p id="time"></p>
<video id="v0" controls tabindex="0" autobuffer preload>
    <source type="video/webm; codecs=&quot;vp8, vorbis&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.webm"></source>
    <source type="video/ogg; codecs=&quot;theora, vorbis&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.ogv"></source>
    <source type="video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.mp4"></source>
    <p>Sorry, your browser does not support the &lt;video&gt; element.</p>
</video>
<div class="slidecontainer">
  <p>Time of video slider:</p>
  <input type="range" min="0" max="1024" value="0" class="slider" id="vidSlider">
</div>

JavaScript

var vid = $('#v0')[0];
var slider = document.getElementById('vidSlider')
linkVideoToSlider();

vid.onplay = vid.onclick = function() {
  vid.onplay = vid.onclick = null;

  setTimeout(function() {
    vid.pause();
    slider.value = vid.currentTime / vid.duration * 100
    vid.currentTime += (1 / 29.97);

  }, 12000);

  setInterval(function() {
    $('#time').html((vid.currentTime * 29.97).toPrecision(5));
    slider.value = vid.currentTime / vid.duration * slider.max;
  }, 100);
};

function linkVideoToSlider() {
  var adjustVideoTime = function() {
    //Note that I divide by 100 twice here because the video is quite long
    setTimeout(function() {
      vid.currentTime = Number.parseFloat(slider.value / 29.97).toFixed(4);
    }, 5);
  }
  slider.oninput = adjustVideoTime
}