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

API question


Hi,
I use JWPLayer 7.8.4 for playing a HLS stream with DVR functionality

I'd like to run custom code at 2 cases:

1)
The player is not set to autoplay, so if viewer loads the page, he has to push play button to start playing. When he pushes play button, then I want to run my custom code.
2)
For any reason the player may go to 'paused' or 'stopped' state. If the viewer pushes play button in these states, my custom code should run again.

I found, that the best jwplayer event for this is on('play'). It is working find, but unfortunatelly the 'play' event fires when the viewer seeks in the live stream. I do not want to run my code then. Unfortunatelly the 'play' event returns the same object, when the viewer first clicks on play or seeks in stream. The only difference is the position on the seekbar.
So I did this:

jwplayerInstance.on('play', function(e) {
if ( ( playerInstance.getPosition() == '0' && e.oldstate == 'buffering' ) || e.oldstate == 'paused' ){
console.log('Here is the custom code!');
}
});

When the playing started first or the player was in stopped state, then the position is 0 and oldstate is 'buffering', so my code will run.
When the player was in 'paused' state before play button was clicked, then oldstate is 'paused', so my code will run.
When viewer is seeking, the position is not 0 and oldstate is 'buffering', so my code will NOT run.

Fortunatelly, if the viewer seeks, the position will never be 0 again, even if he seeks back to live position.

I don't know if I am right and if this is working as I expected.
And I don't know if this is the best way to achieve this.

Thanks for any help.

3 Community Answers

Alex

JW Player Support Agent  
0 rated :

Hi there,

I think the following may accomplish what you are trying to do:

function customFunction() {
  // whatever you want to happen
}

function listenForPlay() {
  playerInstance.once("play", function() {
    customFunction();
  });
}

listenForPlay();

playerInstance.on("pause", function() {
  listenForPlay();
});

Please let me know if you need any more help or have any other questions.

Thank you!

p...

User  
0 rated :

Hi Alex.

Thank you, this is exactly what I need. :)

Alex

JW Player Support Agent  
0 rated :

Not a problem!

This question has received the maximum number of answers.