【问题标题】:YouTube IFrame API doesn't always load?YouTube IFrame API 并不总是加载?
【发布时间】:2015-06-22 17:53:58
【问题描述】:

我见过很多与此类似的问题,但我相信这是不同的。当我尝试在全局范围内定义onYouTubeIframeAPIReady 时,该函数仅在加载页面的大约一半时被调用(如果我不断刷新,有时我会看到控制台消息,有时它不存在)。让我感到困惑的是,这只有时会发生,而且我不会在代码中的其他任何地方调用 onYouTubeIframeAPIReady

我检查过的问题区域:

  • 我在 Github Pages 上运行它(所以它不是本地的)
  • 函数在全局范围内定义

我的代码如下:

window.onYouTubeIframeAPIReady = function() {
    console.log("YouTube API Ready");

    player = new YT.Player('player', { // TODO: Sometimes this doesn't work
        videoId: curVideoId,
        playerVars: {
            controls: 1,
            autoplay: 0,
            disablekb: 1,
            enablejsapi: 1,
            iv_load_policy: 3,
            // modestbranding: 1,
            showinfo: 1
        }
    }); 

    ytLoaded = true;

    if (windowWidth) { // if document loaded first
        resizePlayer();
    }
}

【问题讨论】:

  • 我从未使用过 iframe api,相反,我总是将 iframe 插入到 dom 中。这总是有效的。
  • 嗯,谢谢。我会这样做,但我需要能够操纵播放器(例如视频中的特定时间 .seekTo())

标签: javascript youtube-api youtube-javascript-api


【解决方案1】:

这听起来很可疑,就像您正在通过 DOM 标记中的 src 属性加载 API 库,而不是在页面加载后将其动态添加到 DOM 的建议方式,或者您正在加载库在创建播放器对象所针对的 DOM 元素之前...因为 onYouTubeIframeAPIReady 函数在库自身加载后立即被调用,因此如果您尝试通过标签上的 src 属性加载库,则始终存在这种可能性它会在您的函数实际注册之前或在 DOM 中存在(在这种情况下)“播放器”元素之前加载并调用该函数。工作代码看起来像这样(放置在页面底部附近......绝对在您尝试创建 iframe 的元素下方):

<div id="player"></div>
<script>
 var tag = document.createElement('script');
 tag.src = "https://www.youtube.com/iframe_api";
 var firstScriptTag = document.getElementsByTagName('script')[0];
 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

 var player;
 window.onYouTubeIframeAPIReady = function() {
    console.log("YouTube API Ready");

    player = new YT.Player('player', { 
        videoId: curVideoId,
        playerVars: {
            controls: 1,
            autoplay: 0,
            disablekb: 1,
            enablejsapi: 1,
            iv_load_policy: 3,
            // modestbranding: 1,
            showinfo: 1
        }
    }); 

    ytLoaded = true;

    if (windowWidth) { // if document loaded first
        resizePlayer();
    }
 };
</script>

如果我错了,你是如何加载库的,那么你需要在 Chrome 开发工具或 firebug 中监控以查看何时加载库到 DOM 元素存在的时间。

【讨论】:

  • 就是这样!感谢您还解释了为什么这两个工作方式不同,这真的很有帮助。
  • 遇到这个问题,整个上午都在努力解决。这篇文章是解决方案,我在 doc ready 和 onYouTubeIframeAPIReady 上添加了控制台日志,有时在 doc 准备好之前调用了 onYouTubeIframeAPIReady,并且由于 YT.player 的 div 容器还不存在,它无法创建播放器。所以我添加了在文档就绪侦听器中加载 youtube api 的行,现在一切正常。
【解决方案2】:

你忘记了最后的分号:

window.onYouTubeIframeAPIReady = function() {
    console.log("YouTube API Ready");

    player = new YT.Player('player', { // TODO: Sometimes this doesn't work
        videoId: curVideoId,
        playerVars: {
            controls: 1,
            autoplay: 0,
            disablekb: 1,
            enablejsapi: 1,
            iv_load_policy: 3,
            // modestbranding: 1,
            showinfo: 1
        }
    }); 

    ytLoaded = true;

    if (windowWidth) { // if document loaded first
        resizePlayer();
    }
};

【讨论】:

  • JavaScript 使用自动分号插入,所以这不会是这个问题的原因。但是,最好使用它们。
  • 或者他可以只运行两次程序
  • 感谢您的收获。但这并没有改变行为 - 它有时仍然会加载,而不是其他时间。
猜你喜欢
  • 1970-01-01
  • 2018-05-20
  • 2013-10-24
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 2015-06-24
  • 1970-01-01
相关资源
最近更新 更多