justinribeiro / lite-youtube

The fastest little YouTube web component on this side of the internet. The shadow dom web component version of Paul's lite-youtube-embed.

Home Page:https://www.npmjs.com/package/@justinribeiro/lite-youtube

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove hardcoded autoplay

AleksBeliun opened this issue · comments

Was wondering why were videos playing automatically even if autoplay is set to 0 in params

src="https://www.youtube.com/embed/${this.videoId}?autoplay=1&${this.params}"

Is there a reason why is autoplay is hardcoded to 1?

Is there a reason why is autoplay is hardcoded to 1?

This setting honors what the user is looking to do in two cases:

  1. When the user interacts (clicks) the play button, the iframe is the loaded in that method (addIframe()) and plays based on that user interaction. If you didn't have autoplay=1, they would have to click twice to start the video.
  2. When the intersection observer hits the mark it too calls addIframe() which then invokes the injection which is helpful on mobile to cut down interactions due to constraints (ala, you can't autoplay on mobile devices).

The component has worked like this for quite a while. Can you explain why the double-interaction is a use case for you?

Double interaction is not the case that I am hoping for and I am not experiencing any double click to play in the first place.

My case is a blog article where there are multiple block components that build the final page
The flow is something like this:

  1. Dynamically include lite-youtube script if the page contains any blocks with type video
  2. Populate block templates with article data
  3. All images or any other external content should be loaded lazily so the autoload with IntersectionObserver option comes into play

image

  1. Page is built, served, everyone is happy

All is fine but the video starts playing once the video block comes into view.

Changed the behavior for this case by removing hardcoded autoplay=1, adding autoplay=0 through params attribute, and serving modified lite-youtube script from own server.
Mobile and Desktop play videos with just one click/tap without issues.
image

Feels like autoplay option should have been left for the developer to control and/or maybe to have a default value of 1 that should be overridable with params or a separate tag attribute.

autoload with IntersectionObserver option comes into play

Humm, yeah, on desktop that's an issue. The IntersectionObserver kicks automatically and on desktop, it will autoplay (though on mobile it won't).

Feels like autoplay option should have been left for the developer to control and/or maybe to have a default value of 1 that should be overridable with params or a separate tag attribute.

Can't directly give control on it, otherwise things will get weird for the common cases. I can however update it such that for the intersection observer the autoplay doesn't kick. Let me work up a patch and I'll get a version stamped. That way the behavior is 1:1 with the click interaction and the intersection observer.

Thank you