15

我正在尝试加载 YouTube 的 iframe API。到目前为止,我要做的就是制作和加载播放器。它似乎加载了 API,但随后无法将“YT.Player()”识别为构造函数。在 chrome js 控制台中,我在该行遇到的确切错误是:

    Uncaught TypeError: undefined is not a function 

所以...我到底做错了什么?我已经在这件事上抛出了 console.log 语句,并尝试用几种方式重写它。我尝试将 api 复制到本地文件中。我试过用常规脚本标签加载它。我已经尝试使用他们在https://developers.google.com/youtube/iframe_api_reference的 api 参考中使用的古怪 DOM 修改来加载它。我很确定下面的代码应该可以工作:

    function youtubeAPIReady(script, textStatus, jqXHR)
    {
        player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'CxTtN0dCDaY'
        });
    }

    function readyFunction()
    {
        $.getScript("https://www.youtube.com/iframe_api", youtubeAPIReady);
    }

    jQuery(document).ready(readyFunction);

有什么帮助吗?

4

6 回答 6

14

您可以借用YouTube Direct Lite 中使用的技术来延迟加载 JavaScript,直到明确需要它:

var player = {
  playVideo: function(container, videoId) {
    if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
      window.onYouTubeIframeAPIReady = function() {
        player.loadPlayer(container, videoId);
      };

      $.getScript('//www.youtube.com/iframe_api');
    } else {
      player.loadPlayer(container, videoId);
    }
  },

  loadPlayer: function(container, videoId) {
    new YT.Player(container, {
      videoId: videoId,
      width: 356,
      height: 200,
      playerVars: {
        autoplay: 1,
        controls: 0,
        modestbranding: 1,
        rel: 0,
        showInfo: 0
      }
    });
  }
};
于 2013-08-09T20:16:46.013 回答
9

穷人的解决方案,但是...

function readyYoutube(){
    if((typeof YT !== "undefined") && YT && YT.Player){
        player = new YT.Player('player', {
            ...
        });
    }else{
        setTimeout(readyYoutube, 100);
    }
}
于 2014-12-22T11:37:24.223 回答
7

引用自http://api.jquery.com/jQuery.getScript/

一旦脚本被加载但不一定执行,回调就会被触发。

API 可能在您调用时尚未运行YT.Player()

于 2013-08-11T12:00:26.903 回答
4

我不能代表 jQuery 解决方案,但请尝试使用股票标准 javascript。在任何情况下,您都不必等待加载文档(此代码应位于外面$(document).ready()

// Load the YouTube API 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);

// Create the player object when API is ready
var player;
window.onYouTubeIframeAPIReady = function () {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'CxYyN0dCDaY'
    });
};
于 2013-08-09T02:58:20.890 回答
0

我通过结合 Simon 和 user151496 的方法解决了这个问题。

编码:

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

    // define player
    var player;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '360',
            width: '640'
        });
    }

    $(function () {

       // load video and add event listeners
       function loadVideo(id, start, end) {
          // check if player is defined
          if ((typeof player !== "undefined")) {
            // listener for player state change
            player.addEventListener('onStateChange', function (event) {
                if (event.data == YT.PlayerState.ENDED) {
                    // do something
                }
            });
            // listener if player is ready (including methods, like loadVideoById
            player.addEventListener('onReady', function(event){
                event.target.loadVideoById({
                    videoId: id,
                    startSeconds: start,
                    endSeconds: end
                });
                // pause player (my specific needs)
                event.target.pauseVideo();
            });
        }
        // if player is not defined, wait and try again
        else {
            setTimeout(loadVideo, 100, id, start, end);
        }
      }

      // somewhere in the code
      loadVideo('xxxxxxxx', 0, 3);
      player.playVideo();
   });
</script>
于 2017-07-01T06:53:24.363 回答
-1

从浏览器中删除添加块,然后重试。它对我有用。

于 2017-08-27T15:52:35.457 回答