YouTube播放器iframe API:playVideo不适用于Firefox 9.0.1

Kar*_*rol 7 javascript firefox jquery youtube-api

我有一些YouTube嵌入代码(我只会粘贴代码,这会给我带来麻烦并削减不公开的内容):

console.log(ytplayer);
ytplayer.playVideo();
Run Code Online (Sandbox Code Playgroud)

Chrome和FF上的Console.log向我展示了使用正确方法的好对象,并且存在方法playVideo().它适用于我检查的所有其他浏览器,但它不适用于FF!?更有趣的是,当我使用普通的YouTube播放按钮播放视频时,我可以使用pauseVideo()方法(以及所有其他方法:搜索,控制音量),但我不能使用playVideo()方法......

我使用新的嵌入视频方式:

ytplayer = new YT.Player(player, {
        height: height,
        width: width,
        videoId: videoid,
        allowfullscreen: 'true',
        playerVars: {
            controls: 0,
            showinfo: 0,
            wmode: 'opaque',
            autoplay: (autoplay ? 1 : 0)
        },
        events: {
            'onReady': function () {
                console.log('I am ready');
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

当然'我准备好'是在控制台输出.我不知道我做错了什么,为什么只有FF不工作...没有JS错误,也没有任何线索......希望有人之前遇到过这个问题并解决了!:)

ora*_*ale 11

我遇到了一个非常相似的问题,正在努力寻找答案.我对playVideo()的调用似乎不起作用.

原版的:

$('#play_movie').click(function(){
    $('#video').show();
    if(player)
    {
        if(typeof player.playVideo == 'function')
        {
            player.playVideo();
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是玩家还没有 - 如果我只是花了一点时间来展示,那么这个电话就可以了

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

不知道这是否是你的确切问题,但我希望它对某人有所帮助

  • 有一个'onReady'事件由玩家触发,而不是'onYouTubeIframeAPIReady',你可以使用它,它似乎更正确. (3认同)

Yuj*_* Wu 9

更有效的方法是检查播放器是否准备就绪.如果播放器未准备就绪,请将player.playVideo()排队并在使用onReady事件准备就绪时执行它.要旨

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();
                                             });
} 
Run Code Online (Sandbox Code Playgroud)