Getting an Embedded YouTube Video to Auto Play and Loop

前端 未结 5 1061
醉酒成梦
醉酒成梦 2020-12-08 00:17

I am trying to get a basic Youtube video to auto play and auto loop embedded on a page, but I\'m having no luck.

相关标签:
5条回答
  • 2020-12-08 00:31

    Playlist hack didn't work for me either. Working workaround for September 2018 (bonus: set width and height by CSS for #yt-wrap instead of hard-coding it in JS):

    <div id="yt-wrap">
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
        <div id="ytplayer"></div>
    </div>
    
    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/player_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubePlayerAPIReady() {
        player = new YT.Player('ytplayer', {
          width: '100%',
          height: '100%',
          videoId: 'VIDEO_ID',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }
    
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
        player.mute(); // comment out if you don't want the auto played video muted
      }
    
      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.ENDED) {
          player.seekTo(0);
          player.playVideo();
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
    
    0 讨论(0)
  • 2020-12-08 00:39

    YouTubes HTML5 embed code:

    <iframe width="560" height="315" src="http://www.youtube.com/embed/GRonxog5mbw?autoplay=1&loop=1&playlist=GRonxog5mbw" frameborder="0" allowfullscreen></iframe>​
    

    You can read about it here: ... (EDIT Link died.) ... View original content on Internet Archive project.

    http://web.archive.org/web/20121016180134/http://brianwong.com/blog/2012-youtube-embed-code-autoplay-on-and-all-other-parameters/

    0 讨论(0)
  • 2020-12-08 00:47

    Had same experience, however what did the magic for me is not to change embed to v.

    So the code will look like this...

    <iframe width="560" height="315" src="https://www.youtube.com/embed/cTYuscQu-Og?Version=3&loop=1&playlist=cTYuscQu-Og" frameborder="0" allowfullscreen></iframe>
    

    Hope it helps...

    0 讨论(0)
  • 2020-12-08 00:49

    Here is the full list of YouTube embedded player parameters.

    Relevant info:

    autoplay (supported players: AS3, AS2, HTML5) Values: 0 or 1. Default is 0. Sets whether or not the initial video will autoplay when the player loads.

    loop (supported players: AS3, HTML5) Values: 0 or 1. Default is 0. In the case of a single video player, a setting of 1 will cause the player to play the initial video again and again. In the case of a playlist player (or custom player), the player will play the entire playlist and then start again at the first video.

    Note: This parameter has limited support in the AS3 player and in IFrame embeds, which could load either the AS3 or HTML5 player. Currently, the loop parameter only works in the AS3 player when used in conjunction with the playlist parameter. To loop a single video, set the loop parameter value to 1 and set the playlist parameter value to the same video ID already specified in the Player API URL:

    http://www.youtube.com/v/VIDEO_ID?version=3&loop=1&playlist=VIDEO_ID

    Use the URL above in your embed code (append other parameters too).

    0 讨论(0)
  • 2020-12-08 00:51

    All of the answers didn't work for me, I checked the playlist URL and seen that playlist parameter changed to list! So it should be:

    &loop=1&list=PLvNxGp1V1dOwpDBl7L3AJIlkKYdNDKUEs

    So here is the full code I use make a clean, looping, autoplay video:

    <iframe width="100%" height="425" src="https://www.youtube.com/embed/MavEpJETfgI?autoplay=1&showinfo=0&loop=1&list=PLvNxGp1V1dOwpDBl7L3AJIlkKYdNDKUEs&rel=0" frameborder="0" allowfullscreen></iframe>
    
    0 讨论(0)
提交回复
热议问题