It has been discussed that autoplay is not working on mobile devices Apple as well as Android (Youtube autoplay not working on mobile devices with embedded HTML5 player).
However in order to not destroy my website performance I am only loading images and adding IFrames as soon as a user clicks as demonstrated by this tutorial (https://lean.codecomputerlove.com/make-your-own-lightweight-youtube-player/).
Essential idea is to add only HTML with the data attribute and image:
<div class="video__youtube" data-youtube>
<img src="https://i.ytimg.com/vi/VIDEO_ID/maxresdefault.jpg" class="video__placeholder" />
<button class="video__button" data-youtube-button="https://www.youtube.com/embed/VIDEO_ID"></button>
</div>
And later on add the IFrame via JavaScript as soon as the user clicks:
function createIframe(event) {
var url = event.target.dataset.youtubeButton;
var youtubePlaceholder = event.target.parentNode;
var htmlString = '<div class="video__youtube"> <iframe class="video__iframe" src="' + url + '?autoplay=1" frameborder="0" allowfullscreen></iframe></div>';
youtubePlaceholder.style.display = 'none';
youtubePlaceholder.insertAdjacentHTML('beforebegin', htmlString);
youtubePlaceholder.parentNode.removeChild(youtubePlaceholder);
}
However as Youtube autoplay does not work on mobile devices the user has to click two times for the same action which is inacceptable in terms of user experience. Therefore my idea is to trigger a click within the IFrame as soon as it is loaded (as the user already has clicked). Is there any possibility to achieve this?