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

confusing API with regards to error callbacks

I just spent a lot of time with the basic JW Player API (setup) when I tried to set up some callbacks and I wanted to share my experience.

What I tried to do: I wanted to use the onSetupError callback as documented.

final solution:
var player = jwplayer(contentElement).setup(jwOpts);

In hindsight I see two issues which confused me:

1. It is possible to set up an onReady callback in the setup options.
For example:
jwOpts = {
events: {'onReady': this.handlePlayerReady},
but it is not possible doing the same for onSetupError. If you don't support it (I suspect support for events in setup might be meant as a backwards-compat for JW Player 5 and onSetupError is v6+) I'd like to see an error message if someone sends a callback for an unknown/unsupported event. (I acknowledge that this is uncommon in JS but I think "fail fast" is a good practice.)

I used the approach above because I was used to it with JW Player 5 and to me it seemed like the best way because I assumed that I avoid every possible race condition if the listener is added immediately in setup().

Second issue:
The object returned by the "jwplayer()" call is not the same as the one returned by .setup() but provides an "onSetupError" method which has no effect - but does not bail out either.

Initially I did (simplified):
var player = jwplayer(contentElement);

I see two problems here:
1. In jQuery it is very common just to return a this argument if the operation is not a selector. I assumed that this is also the case for JW Player as "setup()" seems to be just some initialization but should not change the actual object. That assumption was wrong.
2. If the returned instance has to be different for some reason please make it fail for unsupported operations! For example calling ".onSetupError()" should raise an exception (fail fast again) so I notice the problem immediately.

Also I was unable to find an example for onSetupError in the support articles.
now what is wr
this._jwplayer = jwplayer(contentElement).setup(jwOpts);

5 Community Answers

0 rated :

sorry, ignore the last three lines. Unfortunately I can't edit my post.

0 rated :

PPS: The best hint I could find was a Twitter message with a (broken) link to the forums: . You should really have stable URLs...

Ethan Feldman

JW Player Support Agent  
0 rated :

Thanks for the feedback. Do you have an example of what you came up with?

0 rated :

Well, the essentials are just what I wrote in "final solution" (obviously for a full example you need some more boilerplate code but that is trivial, just use the recommended default code).

It's not something spectacular or so. If you do it "right", it works without problems. I wanted to point out that the current JW Player JS API "violated" some of my assumptions which lead to some unproductive debugging.

But maybe I misunderstood what your question was aiming at?

Ethan Feldman

JW Player Support Agent  
0 rated :

I see. Thank you for the feedback. I will pass this along to our player team.

This question has received the maximum number of answers.