Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I listen for the "timeupdate" event from my video.js player?

While I have been able to listen for the "timeupdate" event with a vanilla HTML5 player, I can't seem to get it to work with video.js.

Here's my code:

<!DOCTYPE html>
<html>
<head>
  <link href="video-js.css" rel="stylesheet">
    <script src="video.js"></script>
    <script src="jquery.min.js"></script>
</head>

<body>
    <video id="testvid" class="video-js vjs-default-skin" controls  width="640" height="480" data-setup='{}'>
        <source src="testvideo.mp4" type="video/mp4">
    </video>
</body>

</html>

<script>
videojs('testvid').ready(function(){
    var myPlayer = this;
    $(myPlayer).bind('timeupdate', function(){
        console.log('the time was updated to: ' + this.currentTime);
    });
});
</script>

Am I misunderstanding a basic way that video.js operates?

like image 986
coyote Avatar asked Apr 14 '15 03:04

coyote


People also ask

What is Timeupdate event in JavaScript?

The timeupdate event is fired when the time indicated by the currentTime attribute has been updated. The event frequency is dependent on the system load, but will be thrown between about 4Hz and 66Hz (assuming the event handlers don't take longer than 250ms to run).

What is Player js?

js. A JavaScript library that allows developers to programmatically control video and audio within IFrames across a number of services. Publishers can also expose a JavaScript API for developers to build rich applications with their media.

How do I initialize a video in JavaScript?

js setup methods are used to initialize a video. var myPlayer = videojs('example_video_1'); In the following example, the data-setup attribute tells the Video. js library to create a player instance when the library is ready.

What is time update?

The timeupdate event occurs when the playing position of an audio/video has changed. This event is invoked by: Playing the audio/video. Moving the playback position (like when the user fast forwards to a different point in the audio/video)


1 Answers

You actually CAN register events in callback of ready(), and it is better solution because in previous one registration of event still could be finished after ready(). Use following snippet:

videojs('example_video_1').ready(function () {
    this.on('timeupdate', function () {
      console.log(this.currentTime());
    })
  });
like image 124
Grigory Avatar answered Oct 20 '22 22:10

Grigory