videojs / videojs-contrib-ads

A Tool for Building Video.js Ad Plugins
http://videojs.github.io/videojs-contrib-ads/
Other
384 stars 257 forks source link

FR: Gracefully fall back to click to play when autoplay is disallowed #319

Closed shawnbuso closed 6 years ago

shawnbuso commented 6 years ago

I'm testing on Safari 11 desktop with the IMA SDK plugin. If I try to autoplay unmuted, the browser prevents playback, but the contrib-ads plugin still fires "preroll?" indicating that I should start my pre-roll. Ideally the contrib-ads plugin should detect that autoplay is not allowed and wait until the user clicks the player's play button to fire "preroll?" like it does in a non-autoplay page.

incompl commented 6 years ago

For clarity: "preroll?" is a state in the state machine rather than an event, so the plugin doesn't fire "preroll?". I believe what you're saying is that there is a "play" event even though autoplay was blocked, which results in "readyForPreroll" being fired.

Unfortunately, detecting that autoplay is not allowed is not simple. For an invocation of play you could save the play promise and handle rejection. When using the autoplay attribute, there is no promise. There is a pause event, but we can't wait for a pause event that may never arrive without slowing down the plugin and/or dealing with a certain margin of error.

In discussing your proposal, we realized we may want to use can-autoplay in videojs-contrib-ads to use feature detection for this case. Off the cuff, I think what we'd do is ignore the initial play event if the player has autoplay but can-autoplay says autoplay will fail. One shortcoming of the approach is that successive play events may be due to programatic calls to play rather than user interaction. @gkatsev suggested that maybe video.js could add a flag on the play event indicating whether it came from videojs controls. That would have issues for custom controls though. Input welcome as we consider our options!

gkatsev commented 6 years ago

It may be that we are unable to support the autoplay attribute at all given that browsers basically do not support that use case and recommend users switch to using the play event.

shawnbuso commented 6 years ago

Adding @ocorso so he can follow along.

incompl commented 6 years ago

At this time we've decided not to handle autoplay blocking in this project. Our recommendation at this time is to consider using can-autoplay and avoid using the autoplay attribute.