JWPlayer - start video on mobile device with real button press.

I have an issue where JWPlayer (javascript video player) won't start playing a video if the play method is called from a callback function.
(NOTE:  I am NOT trying to auto start the video on page load.  Everything I am doing is from a user-initiated button press.)
Also, this works correctly on desktop (surprise, surprise).

I have a simple page with a JWPlayer object (excerpt) -

<div id="jw" class="container">
<div id="jw1">Loading the player...</div>
	<script type="text/javascript">
          file: "test.mp4",          
          width: "100%",
          aspectratio: "12:5",
	  autostart: "false"
<input id="btn1" type="button" value="Another Play Button" onclick="fncBuildVideo2();"/>

Open in new window

The player loads correctly and if you push the play button on the player window the video starts fine.
I added a button called "Another Play Button" and it calls "fncBuildVideo()"
When fncBuildVideo() looks like this, the video starts playing when you push the button -

function fncBuildVideo() {

Open in new window

But before I call jwplayer("jw1").play() I need to first make an ajax call to do some server side stuff.
So I placed jwplayer("jw1").play() in the callback function and now the video will not start playing.
This is all initiated from a button click so I am not trying to circumvent the mobile device auto start limits.
But it appears that the mobile device thinks I am trying to circumvent and it refuses to start the video when the play method is called from the callback function.

Any thoughts on how I can do this?

Thanks, Kevin.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your issue is you made you ajax call asynchronous, you need a synchronous ajax call to make it work

imagine an ajax call like a timer like this one :

setTimeout(fncBuildVideo, 300000); // callback function called after 300s

now you click, but the video will start in 300s
I think it's clear now the video should not start because it break the user action

but there's no logic difference between 3ms and 300s

so make your ajax call synchronous,

$.ajax({ async:false,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevinvw1Author Commented:
Thanks the async setting was part of the problem.
It solved the above example perfectly.

But my next step was to dynamically load a different video (based on the ajax call).
I was trying to call the jwplayer().setup method after the button click instead of on page load.
It worked fine on desktop but not on iOS.
In order to get it to work properly I had to keep the jwplayer.setup() method on the page.
Then after I clicked the button and made the ajax call (to get the name of the next video file),
I had to do this - jwplayer().load([{file:" + newVideoToPlay }]);
Then the jwplayer.play() method worked perfectly on iOS.

Thanks for the help.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.