3

I want to capture Total Played/watched time of a video from jwplayer. using below sample code.

jwplayer("player").setup({
    "playlist": [
        {
            "sources": [
                {
                    "default": false,
                    "file": 'https://content.jwplatform.com/manifests/yp34SRmf.m3u8',
                    "label": "0",
                    "type": "hls",
                    "preload": "meta"
                }
            ]
        }
    ],
    "height": "240",
    "width": "100%",
    "aspectratio": "16:9",
    "stretching": "uniform",
    "controls": true,
    "autostart": true
});

jwplayer("player").on('time', function (e) {
    var count = this.getPosition();        
});

Could you please help me doing this.

  • How about calculating the time between `play` and `pause` events? (You may have to remove buffering time) – Cerlin Feb 13 '20 at 05:24
  • Thank you Cerlin, i want to calculate user watched time while the video being played (ontime, forward seek,backward seek). please tell me how to calculate. – aniljayanti Feb 13 '20 at 07:43

3 Answers3

1

You can get the total number of milliseconds the video is played by this simple logic

// In ms
var totalPlayBackTime = 0

setInterval(
    function() {
        if(player.getState() == "playing") {
            totalPlayBackTime = totalPlayBackTime + 500
        }
    },
    500
);

Here the code will be executed every 500 ms to check if the player is playing. If it is, add 500 ms to the total playback time. Read the totalPlayBackTime variable to get the playback time.

This way you don't have to rely on jwplayer events

NOTE: I haven't tested this with ads.

Cerlin
  • 6,622
  • 1
  • 20
  • 28
1

The best way would be to rely on the JW Player events for this, specifically the on('time') event.

let totalTimeWatched = 0;
let previousPosition = 0;
jwplayer().on('time', (e) => {
    const { position } = e;
    totalTimeWatched += (position - previousPosition);
    previousPosition = position;
});

If you do want to count time for ads, as well, you can use the adTime event, which has also has a position property.

EDIT

To account for seeking behavior you can use an additional listener, on('seek') to reset the previousPosition. See below:

let totalTimeWatched = 0;
let previousPosition = 0;
jwplayer().on('time', (e) => {
    const { position } = e;
    totalTimeWatched += (position - previousPosition);
    previousPosition = position;
});

jwplayer().on('seek', (e) => {
    previousPosition = e.offset;
});
boyntoni
  • 106
  • 3
  • As far as i have tested, this is not working. Issue 1: If you seek video, it is counting it as watched. Issue 2: If you have watched 20 minutes and the seek to 2nd minute, it will tell the total watched time as 2 mins. – Cerlin Feb 18 '20 at 05:50
  • 2
    @Cerlin Sorry for the confusion, I have updated the example to account for seeking. – boyntoni Feb 19 '20 at 14:43
0

Why dont you use e.currentTime?

Niels
  • 1
  • I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post. – Yunnosch Nov 27 '21 at 08:34