YouTube player iframe API: playVideo doesn't work on Firefox 9.0.1

后端 未结 4 1839
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 18:16

I\'ve got some YouTube embedding code (I will paste only code which is causing the trouble for me and cut things which are not public):

console.log(ytplayer);
yt         


        
相关标签:
4条回答
  • 2021-02-15 18:30

    A more robust way to do that is to check if the player is ready. If the player is not ready, queue player.playVideo() and execute it when it is ready using the onReady event. Gist

    var playerConfig = {},                 // Define the player config here
        queue = {                          // To queue a function and invoke when player is ready
          content: null,
          push: function(fn) {
            this.content = fn;
          },
          pop: function() {
            this.content.call();
            this.content = null;
          }
        },
        player;
    
    window.onYouTubeIframeAPIReady = function() {
      player = new YT.Player('player', {
        videoId: 'player',
        playerVars: playerConfig,
        events: {
          onReady: onPlayerReady
        }
      });
    };
    
    // API event: when the player is ready, call the function in the queue
    function onPlayerReady() {
      if (queue.content) queue.pop();
    }
    
    // Helper function to check if the player is ready
    function isPlayerReady(player) {
      return player && typeof player.playVideo === 'function';
    }
    
    // Instead of calling player.playVideo() directly, 
    // using this function to play the video. 
    // If the player is not ready, queue player.playVideo() and invoke it when the player is ready
    function playVideo(player) {
      isPlayerReady(player) ? player.playVideo() : queue.push(function() {
                                                   player.playVideo();
                                                 });
    } 
    
    0 讨论(0)
  • 2021-02-15 18:30

    I personally found on preset IFrames, that the API won't work properly if you wouldn't use https://www.youtube.com as the domain. So be careful not to miss on the "www". Otherwise the API will create the player object but will fail to execute methods.

    0 讨论(0)
  • 2021-02-15 18:39

    I came across this post looking for something similar. I found my answer here, by relic180:

    YouTube API - Firefox/IE return error "X is not a function" for any 'player.' request

    Basically, Chrome can initialize youtube embeds even when the divs are hidden (i.e. display:none), but FF and IE can't. My solution was a variant of relic180's:

    I move my player to left:200% or whatever when I want it invisible but getting initialized (and available for other calls to player), then move it back on screen when I need it.

    0 讨论(0)
  • 2021-02-15 18:47

    I was having a very similar issue and was struggling with an answer. My calls to playVideo() didn't seem to work.

    ORIGINAL:

    $('#play_movie').click(function(){
        $('#video').show();
        if(player)
        {
            if(typeof player.playVideo == 'function')
            {
                player.playVideo();
            }
        }
    

    The issue was that the player was not yet available - if I just gave it a bit of time to show up, then the call worked

    $('#play_movie').click(function(){
        $('#video').show();
        if(player)
        {
            var fn = function(){ player.playVideo(); }
            setTimeout(fn, 1000);
        }
    

    Don't know if this is your exact issue, but I hope it helps someone

    0 讨论(0)
提交回复
热议问题