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

Start a playlist at a specific index and calling 'onPlaylistItem' only once


Lets say I have a playlist with multiple items. Whenever a user loads my page, I want to load the playlist, and I want to start the playlist at a specific index (that is maybe submitted by an query parameter).

Well, I could do this:

var player = jwplayer('player').setup({
playlist: somePlaylistArray,
...
...
}).playlistItem(someVarIndicatingTheDesiredIndex)

This works, but the problem lies in the 'onPlaylistItem' callback. This callback is now fired TWICE: once for index 0, and once for the desired index.

I have logic in this callback that I ONLY want to execute once so I'm getting into trouble with this. I don't want to use some stupid 'var firstrun = true' mechanism for obvious reasons.

Any suggestions would be appreciated,

3 Community Answers

jherrieven

User  
0 rated :

Your options are limited here unfortunately.

Both "onPlaylistItem" and "onPlaylist" fire when the player is first set up (contrary to what the documentation states).

What's the obvious reasons behind not using the stupid 'var firstrun = true' mechanism?

James

marko

User  
0 rated :

Thanks for your answer James.

Well, controlling control flow by using these kinds of flags are a major code smell imho. It makes my eyes hurt as well :)

But I guess I'll have to go with it for now...

Todd

JW Player Support Agent  
0 rated :

Other than the onPlaylist() logic, another potential option would be to manipulate your playlist before you instantiate the player.

This question has received the maximum number of answers.