YouTube 播放器 iframe API:playVideo 无法在 Firefox 9.0.1 上运行

发布于 2025-01-05 20:54:34 字数 881 浏览 1 评论 0原文

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

console.log(ytplayer);
ytplayer.playVideo();

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');
            }
        }
    });

当然,“我准备好了”是在控制台输出中。我不知道我做错了什么,为什么只有 FF 不起作用...没有 JS 错误,也没有线索...希望有人以前遇到过这个问题并得到解决!:)

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);
ytplayer.playVideo();

Console.log on Chrome and on FF shows me good objects with correct methods, and method playVideo() exists there. And it works for all other browsers I checked, but it doesn't work on FF!? What is even more interesting, that when I play video using normal YouTube play button then I can use pauseVideo() method (and all the others: seeking, controlling volume), but I can't use playVideo() method...

I use new way of embedding video:

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');
            }
        }
    });

Of course 'I am ready' is in console output. I have no idea what I do wrong and why only FF is not working... There is no JS error, and no clue... Hope someone had this problem before and got it resolved!:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱影子 2025-01-12 20:54:34

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

原文:

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

问题是播放器尚不可用 - 如果我只是给它一点时间出现,那么通话就起作用了

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

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

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

逐鹿 2025-01-12 20:54:34

更可靠的方法是检查玩家是否准备好。如果播放器尚未准备好,请对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();
                                             });
} 

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();
                                             });
} 
沫雨熙 2025-01-12 20:54:34

我发现这篇文章正在寻找类似的东西。我在这里找到了答案,作者:relic180:

YouTube API - Firefox/IE 对于任何“播放器”返回错误“X 不是函数”。 request

基本上,即使 div 隐藏(即 display:none),Chrome 也可以初始化 YouTube 嵌入,但 FF 和 IE 则不能。我的解决方案是 relic180 的变体:

当我希望播放器不可见但已初始化(并且可用于对 player 的其他调用)时,我将播放器移动到 left:200% 或其他位置,然后在需要时将其移回屏幕上。

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.

向地狱狂奔 2025-01-12 20:54:34

我个人在预设的 IFrame 上发现,如果您不使用 https://www.youtube,API 将无法正常工作。 com 作为域。所以要小心不要错过“www”。否则,API 将创建玩家对象,但无法执行方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文