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

the*_*owi 57 youtube mobile html5 overlay fancybox

对我的问题,我有一个链接<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>
Run Code Online (Sandbox Code Playgroud)

Overlay-Window已打开,但视频未自动播放.

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

orz*_*how 57

事实证明,无法在iOS设备(iPhone,iPad,iPod touch)和Android上进行自动播放.

请参阅/sf/answers/569953121//sf/answers/213935431/


小智 12

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

<!-- 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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*ark 10

下面的代码在 iPhone、iPad (iOS13)、Safari (Catalina) 上进行了测试。它能够在所有设备上自动播放 YouTube 视频。确保视频已静音并且playinline参数已打开。这些是使其工作的神奇参数。

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0, user-scalable=yes">
    </head>
  <body>
<!-- 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', {
      width: '100%',
      videoId: 'osz5tVY97dQ',
      playerVars: { 'autoplay': 1, 'playsinline': 1 },
      events: {
        'onReady': onPlayerReady
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
     event.target.mute();
    event.target.playVideo();
  }
</script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)


小智 7

预览 官方声明“由于这一限制,autoplay、playVideo()、loadVideoById()等函数和参数将无法在所有移动环境下工作。

参考: https: //developers.google.com/youtube/iframe_api_reference