Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use YT player API to force autoplay, resolving double-tap bug #109

Merged
merged 10 commits into from
Sep 11, 2022
Prev Previous commit
Next Next commit
flip on yt api path for firefox and safari only
paulirish committed Nov 28, 2021

Verified

This commit was signed with the committer’s verified signature.
dchassin David P. Chassin
commit 0461e7483d9fc599361553cdb454cdcb10494b23
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ <h3>View isolated demos:</h3>
<li><a href="./variants/pe.html">lite-youtube-embed - progressively enhanced</a>
<li><a href="./variants/custom-poster-image.html">lite-youtube-embed - custom poster image</a>
<li><a href="./variants/params.html">lite-youtube-embed - with parameters</a></li>
<li><a href="./variants/forceautoplay.html">lite-youtube-embed - with forceautoplay on</a></li>
<li><a href="./variants/yt.html">normal youtube embed</a>
</ul>
</body>
16 changes: 11 additions & 5 deletions src/lite-yt-embed.js
Original file line number Diff line number Diff line change
@@ -53,9 +53,14 @@ class LiteYTEmbed extends HTMLElement {
// We'd want to only do this for in-viewport or near-viewport ones: https://github.com/ampproject/amphtml/pull/5003
this.addEventListener('click', this.addIframe);

// prep the YT API
if (this.hasAttribute('forceautoplay')) {
this.fetchYoutubeAPI();

// Chrome & Edge have no problem with the basic YouTube Embed with ?autoplay=1
// However Safari and Firefox do not successfully track the user gesture of clicking through the creation/loading of the iframe,
// so they don't autoplay automatically. Instead we must load an additional 300KB (ungz) of JS for the YT Player API
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || navigator.userAgent.includes('Firefox');
Copy link
Owner Author

@paulirish paulirish Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i figure ill have to change this to be some other sniff. don't think there's a good "feature detect" that's equivalent to failing to successfully autoplay. so yeah.. we'll do a UA sniff of some sort.

but this'll need to also include chrome android as well. and perhaps more.

some testing required:

has problems with autoplay on https://lite-youtube-embed-git-master-paulirish.vercel.app/

ios safari
desktop safari
firefox android
chrome android
edge mobile (probably.. untested)

no problems

desktop chrome
desktop firefox
desktop edge (on mac)


if (this.needsYTApiForAutoplay) {
this.fetchYTPlayerApi();
}
}

@@ -100,8 +105,9 @@ class LiteYTEmbed extends HTMLElement {
LiteYTEmbed.preconnected = true;
}

fetchYoutubeAPI() {
fetchYTPlayerApi() {
Copy link
Owner Author

@paulirish paulirish Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alsoo wondering if there's a lazy way to attach... #82 hmmm!

EDIT: see also #146 (comment)

if (window.YT || (window.YT && window.YT.Player)) return;

this.ytApiPromise = new Promise((res, rej) => {
var el = document.createElement('script');
el.src = 'https://www.youtube.com/iframe_api';
@@ -142,7 +148,7 @@ class LiteYTEmbed extends HTMLElement {
params.append('autoplay', '1');
params.append('playsinline', '1');
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (this.hasAttribute('forceautoplay')) {
if (this.needsYTApiForAutoplay) {
return this.addYTPlayerIframe(params);
}

16 changes: 0 additions & 16 deletions variants/forceautoplay.html

This file was deleted.

6 changes: 3 additions & 3 deletions variants/yt.html
Original file line number Diff line number Diff line change
@@ -7,10 +7,10 @@
</head>
<body>

<h3>typical iframe embed</h3>
<h1>typical iframe embed</h1>
<iframe
width="560"
height="315"
width="720"
height="405"
src="https://www.youtube.com/embed/ogfYd705cRs"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"