【问题标题】:Autoplay of YouTube using iframe not working in android and iphones使用 iframe 自动播放 YouTube 无法在 android 和 iphone 中运行
【发布时间】:2018-04-28 06:05:50
【问题描述】:

我正在使用 jquery 通过 iframe 加载 YouTube 视频,以减少初始页面加载时间。使用这种方法——只有视频缩略图与页面一起加载,当用户点击播放按钮时,实际的播放器会加载。但 AutoPlay 无法在 Android 和 Iphone 上运行,尽管它在使用 Windows 的桌面上运行良好。当我搜索这个问题时,结果是“iPhone 和 Android 上的 Chrome 和 Safari 浏览器只允许在用户交互启动时播放 HTML5 视频。它们阻止嵌入式媒体自动播放,以防止通过蜂窝网络进行未经请求的下载。”我想自动播放视频。请帮忙。

这是我的 jquery

document.addEventListener("DOMContentLoaded",
    function() {
        var div, n,
            v = document.getElementsByClassName("youtube-player");
        for (n = 0; n < v.length; n++) {
            div = document.createElement("div");
            div.setAttribute("data-id", v[n].dataset.id);
            div.innerHTML = YouTubeThumb(v[n].dataset.id);
            div.onclick = YouTubeIframe;
            v[n].appendChild(div);
        }
    });

function YouTubeThumb(id) {
    var thumb = '<img src="https://i.ytimg.com/vi/ID/mqdefault.jpg">',
        play = '<div class="play"></div>';
    return thumb.replace("ID", id) + play;
}

function YouTubeIframe() {
    var iframe = document.createElement("iframe");
    var embed = "https://www.youtube.com/embed/ID?autoplay=1";
    iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("allowfullscreen", "1");
    this.parentNode.replaceChild(iframe, this);
}

和 HTML

<div class="youtube-player" data-id="YouTube_Video_ID"></div>

【问题讨论】:

    标签: android jquery html ios youtube


    【解决方案1】:

    试试这个例子:-

      // 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  (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();
      }
    

    【讨论】:

    • 这不是我的意思。我只想上传视频缩略图以及页面和当用户点击播放按钮时要加载的实际播放器,以便我可以对 YouTube 的 javascript 进行不同的解析以提高页面加载速度。
    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2017-09-21
    • 2015-04-11
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多