YT is not defined - Uncaught ReferenceError: [youtube api]

天涯浪子 提交于 2020-07-18 07:20:07

问题


Removed Unwanted Code DEMO

Hey, I am implementing Youtube Video Player Api and i see some sort of error in console.

any how the video plays perfectly, but i want to know why the error is? if i keep the code outside of Skeleton Patterns, there wont be any error.

can someone please shade some light..

Thanks!!

JS :

(function($) {
    $(function(){
        var cVid, ytData1;
        var callFlexSlider = ({         
            embedVideos : function(){
                function explodeSlider(){
                    $('.sliderNew .flexslider').flexslider({
                        slideshow: false
                    });
                    $('.sliderNew #carousel, .sliderNew .flex-direction-nav').hide();
                }
                function explodeShow(){
                    $('.sliderNew .flexslider').flexslider({ 
                        slideshow: true
                    });
    $('.sliderNew #carousel, .sliderNew .flex-direction-nav').show();
                }
                function getArtistId() {
                    return window.artist_id;
                }
                function loadPlayer() {
                    if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
                        var tag = document.createElement('script');
                        tag.src = "https://www.youtube.com/iframe_api";
                        var firstScriptTag = document.getElementsByTagName('script')[0];
                        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
                        window.onYouTubePlayerAPIReady = function() {
                            onYouTubePlayer();
                        };
                    }
                }
                var player;

                function onYouTubePlayer() {
                    player = new YT.Player('player', {
                        height: '335',
                        width: '940',
                        videoId: getArtistId(),
                        playerVars: {
                            controls: 1,
                            showinfo: 0,
                            rel: 0,
                            showsearch: 0,
                            iv_load_policy: 3
                        },
                        events: {
                            'onStateChange': onPlayerStateChange,
                            'onError': catchError
                        }
                    });
                }
                var done = false;

                function onPlayerStateChange(event) {
                    if (event.data == YT.PlayerState.PLAYING && !done) {
                        done = true;
                    } else if (event.data == YT.PlayerState.ENDED) {
                        location.reload();
                        event.target.destroy();
                    }
                }

                function onPlayerReady(event) {
                    console.log('playerReady Event')
                }

                function catchError(event) {
                    if (event.data == 100) console.log("...");
                }

                function stopVideo() {
                    //alert('asdsad');
                    //player.destroy();
                    player.stopVideo();
                    explodeShow();
                }

            },
            init : function(){
                this.embedVideos();
            }

    }); 
    callFlexSlider.init();
    });
})(jQuery);

回答1:


Call this loadPlayer(); inside document.ready function and try.

  function loadScript() {
                        if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
                            var tag = document.createElement('script');
                            tag.src = "https://www.youtube.com/iframe_api";
                            var firstScriptTag = document.getElementsByTagName('script')[0];
                            firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
                        }
                    }

                    function loadPlayer() {
                            window.onYouTubePlayerAPIReady = function() {
                                onYouTubePlayer();
                            };
                    }


$(function () {
  loadScript();
})



回答2:


you can always define it manually on the top of your script links :

<script type="text/javascript" id="www-widgetapi-script" src="https://s.ytimg.com/yts/jsbin/www-widgetapi-vflS50iB-/www-widgetapi.js" async=""></script>
<script src="https://www.youtube.com/player_api"></script>



回答3:


This is the way how I managed to make the iframe api work:

function loadVideo() {
  window.YT.ready(function() {
    new window.YT.Player("video", {
      height: "390",
      width: "640",
      videoId: "M7lc1UVf-VE",
      events: {
        onReady: onPlayerReady,
        onStateChange: onPlayerStateChange
      }
    });
  });

  function onPlayerReady(event) {
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {
    var videoStatuses = Object.entries(window.YT.PlayerState)
    console.log(videoStatuses.find(status => status[1] === event.data)[0])
  }
}

$(document).ready(function() {
  $.getScript("https://www.youtube.com/iframe_api", function() {
    loadVideo();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="video"></div>


来源:https://stackoverflow.com/questions/31065032/yt-is-not-defined-uncaught-referenceerror-youtube-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!