Rob
Rob

Reputation: 738

JWPlayer Prevent SKipping forward unless already watched

I'm using JWPlayer 5.4 and it's setup on the page using the javascript API.

What I'd like to do is make it so that users can fastforward/rewing via the seek bar ONLY if they have already played that part of the video.

So, if a user is watching the video for the first time they can't skip beyond the current position, however they can seek forward and back behind where the video played up until.

I'm struggling with the API onTime events etc. to try and work out the Math to make this work.

Does anyone know how this could be done.

Thanks

Upvotes: 5

Views: 7765

Answers (3)

alex wangombe
alex wangombe

Reputation: 1

Here's what worked for me

var maxPlayPosition = 0.0;
jwplayer().on('time', function (event) {
        var position = jwplayer().getPosition();

        if (currentposition < maxPlayPosition + 1 && currentposition > maxPlayPosition) {
            maxPlayPosition = currentposition;
        }
        if (currentposition > maxPlayPosition) {
            jwplayer().seek(maxPlayPosition);
        }
});

Upvotes: 0

Duffle McTruffle
Duffle McTruffle

Reputation: 21

Had been using @mal's answer for a while, but found it was breaking for some edge cases (e.g. click and drag). Could be because we're on JW player 8?

Anyway, got a modified solution that covers them pretty well if anyone is still looking for answers to this. Relies on the seeked handler which fires after the seek occurs.

var seeking = false;
var maxPlayPosition = 0;

jwplayer().on('time', function (event) {
    if (!seeking) {
        maxPlayPosition = Math.max(event.position, maxPlayPosition);
    }
}).on('seek', function (event) {
    seeking = true;
}).on('seeked', function (event) {
    var pos = jwplayer().getPosition();
    if (pos > maxPlayPosition) {
        jwplayer().seek(maxPlayPosition);
    }
    seeking = false;
});

Upvotes: 2

mal
mal

Reputation: 71

I found this a while back, probably on the JWplayer forum. I think I added a bit about the playlist. So just in case you or others are still looking for an answer, consider adding stuff like:

var maxPlayPosition = 0.0;
var seeking = false;

jwplayer().onTime(function(event) 
{
    if (!seeking) 
    {
        maxPlayPosition = Math.max(event.position, maxPlayPosition); 
    }
})
.onPlaylistItem(function()
{
    maxPlayPosition = 0.0;
})   // consider using only if you have playlists
.onSeek(function (event) 
{
    if (!seeking) 
    {
        if (event.offset > maxPlayPosition) 
        {
            seeking = true;
            setTimeout(function ()
            {  
               jwplayer().seek(maxPlayPosition);
            }, 100);
        }
    } 
    else 
    {
        seeking = false;
    }   
 });

Upvotes: 7

Related Questions