Search code examples
javascriptvideo-streamingvideo.js

use handleSeeked() function in videojs


I'm using videojs to play videos on the site and I'm trying to create log on video streams but I have difficulty logging users seeking in videos. I need seek start and end time and it seems that the built in handleSeeked() function in videojs does the job but I can't get it to work. Does anybody know how to use this function?

Update

It seems that this question is about the same problem but the solutions aren't fully working and each one has a little problem.


Solution

  • I was able to get what I wanted with the following code but I had to handle everything on my own so if there's still a better way to do this I'd be happy if you could help me.

        var previousTime = 0,
        currentTime = 0,
        completeTime = 0,
        position = 0;
    
    player.on('timeupdate', function () {
        previousTime = currentTime;
        currentTime = Math.floor(player.currentTime());
    
        // save 'position' so long as time is moving forward with each update
        if (previousTime < currentTime) {
            position = previousTime;
            console.log('pos',position)
    
            previousTime = currentTime;
            console.log('pre',previousTime)
        }
    });
    
    player.on('seeked', function () {
        completeTime = Math.floor(player.currentTime());
        console.log("User came from: " + position);
        console.log("User ended at: " + completeTime);
        position= Math.floor(player.currentTime())
    });