Dan Dan - 12 days ago 4
Javascript Question

Get currentTime from video.js

I am using the docs to get the current time of my video but it just outputs

0
when I do a console log whcih makes sense as when I page load the video is at
0
is there a way I can like watch the time and if I pause it or at the end get the video time?

the reason why I ask I am trying to get the time to monitor if my users have actually watched it to the end and I have not used this library before:

<video id="videoPlayer" class="video-js vjs-default-skin vjs-controls-enabled" poster="http://camendesign.com/code/video_for_everybody/poster.jpg" data-setup="{}" controls="">
<source src="sample.mp4" type="video/mp4">
<p class="vjs-no-js">Javascript was disabled or not supported</p>
</video>


<script type="text/javascript">

var myPlayer = videojs('videoPlayer');

var whereYouAt = myPlayer.currentTime();
console.log(whereYouAt);

</script>


console.log gives me
0

Answer

Use timeupdate event

Fired when the current playback position has changed * During playback this is fired every 15-250 milliseconds, depending on the playback technology in use.

var myPlayer = videojs('videoPlayer');
myPlayer.on('timeupdate', function() {
  console.log(this.currentTime());
});
Comments