1

Is it possible to delay the autoplay of an iframe embeded YouTube player?

This is the the embed code I have:

<iframe width="560" height="315" src="https://www.youtube.com/embed/iejOAPyooXs?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe>
Cozzmy Cosmin
  • 140
  • 1
  • 3
  • 11

3 Answers3

2

Updated my answer to a better one using youtube player API for more option and dynamic output. It has many option to select and you could easily control it using Javascript. I hope this could help. Credits to @wasikuss posted answer.

  // Load the IFrame Player API code asynchronously.
    setTimeout(function() {
        player.playVideo();
    }, 20000);
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/player_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
    var player;
    function onYouTubePlayerAPIReady() {    
        player = new YT.Player('ytplayer', {
          height: '315',
          width: '560',
          videoId: 'iejOAPyooXs'
        }); 
    }

Reference https://developers.google.com/youtube/player_parameters

yamidemichaos
  • 110
  • 1
  • 10
1

You could use JavaScript to facilitate the delay. Initially display just the video's thumbnail on the page. In the JavaScript, wait for 20 seconds and then replace the preview with the video iframe.

This other post describes how to show just the thumbnail: Youtube Video Thumbnail with Youtube API.

Community
  • 1
  • 1
Sildoreth
  • 1,883
  • 1
  • 25
  • 38
0

Sildoreth's solution has one pitfall: user cannot play video himself. More properly solution is to use YouTube IFrame API ( https://developers.google.com/youtube/iframe_api_reference ). After 20s just check video state ( player.getPlayerState() ) and if it is video cued ( 5 ) call player.playVideo()

JSFiddle

wasikuss
  • 969
  • 8
  • 13