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

onPlay incorrectly firing multiple times

In Safari, Firefox, and Chrome on a Mac desktop, the first time a user clicks the play button, it appears that jwplayer().onPlay() appears fires twice, even though the user has clicked the play button only once. This does not happen when a user pauses a video and then clicks the play button to resume. Is there a way to get onPlay to fire only once?

Here is a sample:

1 Community Answers


JW Player Support Agent  
0 rated :

onPlay is intended to fire each time the player enters the PLAYING state. This can have some unwanted or unexpected outcomes if you are only trying to use the onPlay() call to do something only the first time a video starts playing. An easy way to use the onPlay() call to only do something once is via this method:

1) Declare a new variable and set it to false
2) Inside your onPlay() function, use an if() statement to check if the variable is false
3) Inside the if() statement, add your Javascript code
4) Be sure to set your variable to true so the code inside the if statement will not run again

onPlay() will continue to fire, but since your if() statement is no longer true, the code inside will only run once.

Here’s a code example:
var only_once = false;
if (only_once == false) {
only_once = true;
//Add your Javascript code here

This question has received the maximum number of answers.