Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

clear getPosition()


I am using a playlist via Ajax to allow the user to view all movies in our titles. I'm using getPosition() to get the current position for onPlay and onPause events. Cool. However, when I play a new movie in the onPlay event the getPosition() method outputs the last position from the last movie played. If I pause the movie it correctly shows the current position. So, my question:

How can I unset and/or force the getPosition value to 0 when a new movie is played. Seems like this may be a bug in the player?

Thanks

Chris

22 Community Answers

jherrieven

Best Answer 

There are two ways around this.

I'll email you directly Mike to walk you through how you can do this as your requirements are slightly more involved than we have discussed here.

For anyone else who is interested, the simplest way to get an accurate "getPosition()" result from the "onPlay" event is to defer to the "onTime" event.

In HTML5 mode, "onPlay" is fired based on front end interaction logic and doesn't accurately reflect what is going on with the underlying <video> tag.

"onTime" is driven directly by the <video> tag and so is a more accurate reflection of the current video's state of play.

/* ********************************************************* */
/* Create a helper function
/* ********************************************************* */
var deferedEvents = {};
function deferToOnTime(eventName, callback){
deferedEvents[eventName] = callback;
};

/* ********************************************************* */
/* Defer the getPosition() call in onPlay event
/* ********************************************************* */
jwplayer().onPlay(function(){

deferToOnTime("onplay", function(){
var position = jwplayer().getPosition();
console.log("play position: " + position);
});

/* Other stuff can happen here */
});

/* ********************************************************* */
/* Process any defered requests within onTime
/* ********************************************************* */
jwplayer().onTime(function(event) {
for(var defered in deferedEvents){
if(typeof deferedEvents[defered] === "function"){
deferedEvents[defered]();
deferedEvents[defered]=null;
}
}
});

/* ********************************************************* */

Now when you load a new video from a playlist, this will report the position of the video at the first timed event after onPlay was actioned - most likely "0.2" rather than whatever the previous video position was.

This is only a problem in HTML5 mode, and so you'd ideally put conditional logic in accordingly.

James Herrieven

View in conversation

mike

User  
0 rated :

BTW, here is how I'm getting the position:

var position = jwplayer().getPosition();

and I *have* tried the following on play of a new movie:

var positon = "";
and also
var position = undefined;

No dice, the call to the getPosition method still returns the last position from the previous movie.

Ethan Feldman

JW Player Support Agent  
0 rated :

Do you have a link to where you are running this?

mike

User  
0 rated :

I have set up a public page for you to test this with:

http://learndev.infiniteskills.com/jwplayer.html

I'm writing to the console the position retrieved by the getPosition method. You'll see that it works fine as you play and pause a movie. However, when you select a new movie from the course contents menu, the initial position is still the last position from the previous movie. To test, load a new movie and you'll see the old position in the console. Then immediately pause the movie and you'll see the new *correct* value.

Ethan Feldman

JW Player Support Agent  
0 rated :

Can you update to 6.9?

mike

User  
0 rated :

OK, I have updated. Still the same issue...

Ethan Feldman

JW Player Support Agent  
0 rated :

Odd, I just checked a demo here – http://support.jwplayer.com/customer/portal/articles/1406722-rss-playlist-embed

In developer tools, I ran jwplayer().getPosition(); a bunch of times, and it always updates, then I changed to the next playlist item, ran jwplayer().getPosition(); again, but the position was reset back to the start.

mike

User  
0 rated :

Well it's not working here, even if I use the forward and back buttons on the controller...

mike

User  
0 rated :

It may be that when you run jwplayer().getPosition(); from the console it grabs it from that point, but I don't see a way using that demo that you can run it at the exact second that the movie starts playing.

Ethan Feldman

JW Player Support Agent  
0 rated :

Here is some sample code that always logs the position to the console, and it resets to 0 when changing playlist items:

<html> <head> <title>Test Page</title> </head> <body> <script type="text/javascript" src="http://p.jwpcdn.com/6/9/jwplayer.js"></script><div id="container"></div> <script> jwplayer("container").setup({ playlist: "http://content.jwplatform.com/feeds/13ShtP5m.rss", width: 640 }); </script><script type="text/javascript"> jwplayer().onTime(function(event) { console.log(event.position); }); </script></body> </html>

Gist – https://gist.github.com/emaxsaun/04a99aeef25db57c13ed

mike

User  
0 rated :

What part of that code will always reset to 0? Looks to me that this is merely an output function. I added this code and tested it and while I can see the output in the console from it, it still doesn't reset when a new movie plays. See screenshot:

https://www.dropbox.com/s/bmnthecexagys1i/Screenshot%202014-08-07%2014.21.02.png

The "play item" is the indicator of a new movie playing. You'll see that it still has the last position from the previous movie.

Ethan Feldman

JW Player Support Agent  
0 rated :

It should do it by default (resetting it on a new item). You do see it resetting with the code I provided, right?

mike

User  
0 rated :

Nope. If you open my page or look at the screen shot you'll see that the new movie plays but still shows the position from the last movie.

It is this bit of code:

jwplayer().onPlay(function(){

// get the object that we need
var item = jwplayer().getPlaylistItem();
var index = playlistManager.getPlaylistIndex(jwplayer());
var position = jwplayer().getPosition();
console.log("position: " + position);

in the onPlay I'm again getting the position, but it is still the position marker from the last movie played.

Ethan Feldman

JW Player Support Agent  
0 rated :

I am talking about the code I provided to you. I ran it locally, and it resets the console log when I change items.

mike

User  
0 rated :

Where did you insert your code amongst my code to make it work?

Ethan Feldman

JW Player Support Agent  
0 rated :

I didn’t, I was just showing you that in my code, if you open the console, it always resets to 0 when the playlist item changes. Did you try it?

mike

User  
0 rated :

Yes I did and I posted my results. See this screenshot:

https://www.dropbox.com/s/5f77enl5v86w0v0/Screenshot%202014-08-07%2017.30.27.png

Here is the snippet of code

jwplayer().onTime(function(event) {
console.log(event.position);
});

jwplayer().onPlay(function(){

// get the object that we need
var item = jwplayer().getPlaylistItem();
var index = playlistManager.getPlaylistIndex(jwplayer());
var position = jwplayer().getPosition();
console.log("position: " + position);
...

You can see that the value beside "position" in the console for each new movie is the same value as the last position reported by the onTime method

Ethan Feldman

JW Player Support Agent  
0 rated :

I mean just my entire setup code, don’t mix it with yours.

mike

User  
0 rated :

I tried that but it doesn't tell me what the position is at the start of the movie. I need to get the position of 0 when a new movie plays, your code merely shows the position as a movie is playing but does NOT report at any time a position of 0.0. First position always seems to be 0.2.

I think you need to wrap that in an onPlay and then call the getPosition like I have to see what it is at the second that a movie plays.

jherrieven

User  
0 rated :

Indeed.

@Ethan if you add the following to the console on your example page (http://support.jwplayer.com/customer/portal/articles/1406722-rss-playlist-embed):

jwplayer().onPlay(function(){
console.info('Position: ' + jwplayer().getPosition());
});

Let the first item play for a bit then switch to the next item and you will see what Mike is talking about.

Ignoring the fact onPlay is fired twice (i know - i'm like a broken record!), it appears that the underlying trigger for the onPlay event is happening before the current video timing event has had a chance to write its new positional info - it's an issue with the sequence of event triggers bound to the underlying <video> element - it's not a problem in Flash mode.

@Mike - can you simply set a global "positional" variable to zero when you load a new video - then use the onTime event to update this, and interrogate this variable when you want to know the current position?

James Herrieven

Ethan Feldman

JW Player Support Agent  
0 rated :

I see what you mean, thanks James.

mike

User  
0 rated :

James, I've having difficulty implementing your suggestion (I emailed you about this earlier today BTW).

Have either you or Ethan tried this with the code in my page?

jherrieven

Best Answer  User  
0 rated :

There are two ways around this.

I'll email you directly Mike to walk you through how you can do this as your requirements are slightly more involved than we have discussed here.

For anyone else who is interested, the simplest way to get an accurate "getPosition()" result from the "onPlay" event is to defer to the "onTime" event.

In HTML5 mode, "onPlay" is fired based on front end interaction logic and doesn't accurately reflect what is going on with the underlying <video> tag.

"onTime" is driven directly by the <video> tag and so is a more accurate reflection of the current video's state of play.

/* ********************************************************* */
/* Create a helper function
/* ********************************************************* */
var deferedEvents = {};
function deferToOnTime(eventName, callback){
deferedEvents[eventName] = callback;
};

/* ********************************************************* */
/* Defer the getPosition() call in onPlay event
/* ********************************************************* */
jwplayer().onPlay(function(){

deferToOnTime("onplay", function(){
var position = jwplayer().getPosition();
console.log("play position: " + position);
});

/* Other stuff can happen here */
});

/* ********************************************************* */
/* Process any defered requests within onTime
/* ********************************************************* */
jwplayer().onTime(function(event) {
for(var defered in deferedEvents){
if(typeof deferedEvents[defered] === "function"){
deferedEvents[defered]();
deferedEvents[defered]=null;
}
}
});

/* ********************************************************* */

Now when you load a new video from a playlist, this will report the position of the video at the first timed event after onPlay was actioned - most likely "0.2" rather than whatever the previous video position was.

This is only a problem in HTML5 mode, and so you'd ideally put conditional logic in accordingly.

James Herrieven

This question has received the maximum number of answers.