Youtube 自动播放不适用于带有嵌入式 HTML5 播放器的移动设备

新手上路,请多包涵

对于我的问题,我有一个链接 <a href="http://www.youtube.com/embed/YT-ID" class="overlay_video"></a> 。我想通过单击 fancybox 覆盖窗口中的链接来播放视频。这不是问题。问题是参数,例如“自动播放”或“自动隐藏”。

以下链接无效:

 <a href="http://www.youtube.com/embed/YT-ID?autoplay=1" class="overlay_video"></a>

覆盖窗口已打开,但视频未自动播放。

编辑:我想在移动设备上使用 HTML5 播放器。在桌面浏览器上,它可以使用参数,但不能在移动设备上使用。

原文由 theowi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 661
2 个回答

看看下面的代码。测试并发现在移动和平板设备上工作。

 <!-- 1. The <iframe> (video player) will replace this <div> tag. -->
<div id="player"></div>

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_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 onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }

  // 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.
  var done = false;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING && !done) {
      setTimeout(stopVideo, 6000);
      done = true;
    }
  }
  function stopVideo() {
    player.stopVideo();
  }
</script>

原文由 Zubi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题