如何在HTML上的YouTube视频上添加自动播放和循环播放?

时间:2018-09-20 10:34:00

标签: html html5 youtube autoplay

我无法在要放到HTML网站上的视频中添加自动播放功能。添加?autoplay = 1或&autoplay = 1无效。 -与循环相同

<div class="videoContainer">
    <iframe class="videoContainer__video" width="560" height="315" src="https://www.youtube.com/embed/HWl8XAOQnTg?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen> </iframe>
</div>

3 个答案:

答案 0 :(得分:0)

您好,我的朋友,您可以找到正确嵌入youtube视频的正确步骤;

  1. 在计算机上,转到您要嵌入的YouTube视频。
  2. 在视频下,单击“共享”。
  3. 点击嵌入。
  4. 从出现的框中复制HTML代码。
  5. 将代码粘贴到您的博客或网站HTML中。

要允许视频自动播放,请使用此allow="autoplay; encrypted-media"并在代码中添加autoplay=1。按照上述步骤,首先通过youtube嵌入视频,然后将autoplay=1添加到视频网址中。

我知道您已经说过您已经尝试添加<script> //var variable = <?php echo json_encode($_SESSION['abc']); ?>; </script> ,但这可能会帮助您按照我的步骤重试。让我知道你过得怎么样。

答案 1 :(得分:0)

在这里使用: 打开下面的代码笔链接 它也会循环播放您的视频:(运行视频的缓存版本,查看@Turnip Comment)

  

;&autoplay = 1;&loop = 1;&playlist = HWl8XAOQnTg

https://codepen.io/GAUTAMRAJU15/pen/MqLJRa

<iframe class="videoContainer__video" width="560" height="315"     src="https://www.youtube.com/embed/HWl8XAOQnTg?rel=0&amp;&amp;showinfo=0;&autoplay=1;&loop=1;&playlist=HWl8XAOQnTg"> </iframe> 

答案 2 :(得分:0)

不依赖似乎不适用于我的参数-2018年9月的解决方法(奖金:通过CSS为#yt-wrap设置宽度和高度,而不是在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>