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

Error Handling Failed Autoplay Attempt. DOMException: play() can only be initiated by a user gesture


When I attempt to autoplay and it fails, I receive the error " Uncaught (in promise) DOMException: play() can only be initiated by a user gesture."

I would like to catch the promise rejection and make the player revert to Click To Play on failed autoplay attempts. When I try to catch the promise rejection ( jwplayer().play().catch(err => console.log(err)); I receive the error "Uncaught TypeError: jwplayer(...).play(...).catch is not a function". I also want to note that on failed play attempts, the state of the player remains "playing" and the jwplayer().on('playAttemptFailed') callback is not called.

What is the proper way to handle failed autoplay attempts?

8 Community Answers

Timoor Kurdi

JW Player Support Agent  
0 rated :

Hi,

My name is Timoor Kurdi and I’m a Support Engineer at JW Player.

We have a playAttemptFailed event handler that you can use – https://developer.jwplayer.com/jw-player/docs/javascript-api-reference/#jwplayeronplayattemptfailed

Something to note is that in the instances mentioned above, that is intended behavior as browsers will usually suppress any non-viewer interaction attempts to start the video.

Hope this helps!

Regards,
Timoor Kurdi
Support Engineer

t...

User  
0 rated :

Hi Timoor, 

Thanks for reaching out. I’ve implemented your suggestion and do not see the ‘playAttemptFailed’ callback fire on a failed autoplay. Here is a test link showing our experience.  http://bit.ly/2Frg4HT

In the index.html file, the ‘playAttemptFail’ event handler is set up to log “Play attempt failed”. When this runs, I see the DOMException error in the console but not console.log set up in the callback.

I also noticed that on a failed autoplay attempt, jwplayer().getState() will still return “playing” even though the player is frozen. Is it possible that the callback is not being fired due to the player’s state not updating? 

Best,

Joe

t...

User  
0 rated :

Hi Timoor,

After further investigation, I've discovered that I am not able to see the event handlers fire for preroll ad content, while it works as expected for the subsequent media file. Is there a way I can get these callbacks to work for preroll ads?

Regards,

Joe

Timoor Kurdi

JW Player Support Agent  
0 rated :

It depends on the event handler, some event handlers work in conjunction with the ads such as jwplayer().on(‘adComplete’), jwplayer().on(‘adError’) etc. For a full list, please refer to our API documentation – https://developer.jwplayer.com/jw-player/docs/javascript-api-reference/

t...

User  
0 rated :

Hi Timoor,

Thanks for your response. After reviewing the API documentation, I am still unable to find an event handler that fires on a failed autoplay attempt for a pre-roll advertisement (on 'adError' doesn't seem to fire after receiving the error "play() can only be initiated by a user gesture."). How would you recommend I proceed? The error message says its an uncaught error in a promise, is there a way to handle the promise rejection to catch this error?

Timoor Kurdi

JW Player Support Agent  
0 rated :

The ‘adError’ event handler will fire if it fits the possible causes listed in the documentation, I am not fully versed on promises so it would be hard for me to give you a definitive answer on that. The browsers will block any action that is not initiated by a user gesture such as autoplaying videos when they are not muted etc.

What browser are you using? What does your setup look like? Some browsers do not allow promises – https://caniuse.com/#feat=promises

t...

User  
0 rated :

Its not clear, but I'm guessing the 'adError' callback will fire when there errors related to a VAST tag, but not from an attempted autoplay. I'm still unable to find a good event handler for this.

I'm using a browser that supports promises, and can catch rejected promises with the HTML5 video API - but not with JWPlayer. I assumed that I would be able to do this since the error states "Uncaught (In Promise)", however trying to catch this raises another error " "Uncaught TypeError: jwplayer(...).play(...).catch is not a function".

Do you have any other recommendations for handling errors generated from a failed preroll autoplay that was not initiated from a user gesture? Ideally we would like to always attempt to autoplay ( in case the user has previously interacted with the domain ) - and default to click to play when this error is raised.

Timoor Kurdi

JW Player Support Agent  
0 rated :

Those were just a few examples that we have listed there, but I would expect any adError to get flagged in that scenario and that is usually the event handler that we recommend when trying to catch errors generated by ads.

This question has received the maximum number of answers.