5

It is possible to autoplay a regular YouTube video in fancybox by appending ?autoplay=1 to the video URL:

<a class="lightbox" href="https://www.youtube.com/embed/zRwBVYjUBOc?autoplay=1">video</a>

… and …

$(document).ready(function(){
    $("a.lightbox").fancybox();
});

Unfortunately in some browsers (Chrome, Safari) this doesn't work if the "privacy-enhanced mode" is enabled (youtube-nocookie.com):

<a class="lightbox" href="https://www.youtube-nocookie.com/embed/zRwBVYjUBOc?autoplay=1">privacy-enhanced mode video</a>

The option autoplay=1 is not fully supported on youtube-nocookie.com.

How can I still have the video automatically played in the lightbox with privacy-enhanced mode?

Edit (Mar 23): Firefox, Chrome and Safari start playing the video immediately on https://www.youtube.com/embed/zRwBVYjUBOc?autoplay=1 (also without fancybox – just open the url). I found out that Firefox still starts to play the video on https://www.youtube-nocookie.com/embed/zRwBVYjUBOc?autoplay=1 , only Safari and Chrome don't.

Ben
  • 811
  • 12
  • 28
  • 1
    Could this be cause of the issue? - https://developers.google.com/web/updates/2017/09/autoplay-policy-changes – Janis Mar 13 '19 at 14:38
  • @Janis I don't know if Google's decision has something to do with this. I guess it may be related to the european General Data Protection Regulation. A solution to my issue might be something via `afterLoad : function() {}` of fancybox. The user has already clicked on a link to open the video in fancybox, so this is no autoplay on load, but on click. – Ben Mar 13 '19 at 15:36
  • @Ben maybe I misunderstood your issue but this is a working example with the two modes: codesandbox.io/s/y0kmrk51lj – Fraction Mar 22 '19 at 13:50
  • @Fraction Thanks! I first tried it with Safari: my first click on "privacy-enhanced mode video" opened fancybox, but the video did not start playing. After closing fancybox and clicking on the same link again, it worked: Fancybox opened and the video started to play. In Firefox it worked immediately on first click. Thanks to your example I found out that the ?autoplay=1 parameter works in Firefox, when I open the URL https://www.youtube-nocookie.com/embed/zRwBVYjUBOc?autoplay=1 , but not in Chrome or Safari. – Ben Mar 22 '19 at 22:08
  • If you open the link in a new tab (middle click the link with your mouse) it works. If you open it in the same tab, it doesn't work... I'm in Chrome – Rojo Mar 23 '19 at 15:53
  • @Rojo: I can't repoduce this. Openening this Link ... https://www.youtube-nocookie.com/embed/zRwBVYjUBOc?autoplay=1 ... in a new tab (chrome, either regular or incognito mode) does not start playing the video. – Ben Mar 23 '19 at 16:24
  • @Ben just add an answer yourself saying it's impossible and mark it as correct so other people don't come and add other random answers – Rojo Mar 24 '19 at 14:58
  • @Rojo I’m not sure if it really is impossible and still try to find a solution. – Ben Mar 24 '19 at 17:37

1 Answers1

0

Not entirely sure how this youtube-nocookie is supposed to work but you can surely trigger a click with javascript...

$('video').click();

If the video is in an iframe, you should still be able to do this;

$('iframe').contents().find('video').click();
Abdul Sadik Yalcin
  • 1,744
  • 2
  • 19
  • 50
  • I think you misunderstood the problem. I don't want the user to click in the video. When the user clicks on the the link this opens a lightbox and generates an iframe with the video. I would like to start playing the video immediately without any further user interaction. – Ben Mar 22 '19 at 21:54
  • 1
    No, I perfectly understood the question. You have misunderstood the answer. "Triggering" a "click" means, doing it by code without a user interaction. – Abdul Sadik Yalcin Mar 23 '19 at 00:49
  • Sorry. You're right, it was me who misunderstood it. I tried your example, but it does not work (Safari and Chrome): https://codesandbox.io/s/jnzwqr6q7w – Ben Mar 23 '19 at 08:40
  • I just found out that the initial problem does not affect all browsers. See my edit above. – Ben Mar 23 '19 at 09:15
  • Can you setup a fiddle with fancybox and you're video - I'll have a look. – Abdul Sadik Yalcin Mar 25 '19 at 15:54