【问题标题】:Win7 IE9 youtube player js object doesn't have any methodsWin7 IE9 youtube player js 对象没有任何方法
【发布时间】:2016-04-20 16:00:25
【问题描述】:

我的获取 Youtube 播放器的代码在 Win7 上除 IE9 之外的所有浏览器中都有效:

var myPlayer;
window.onYouTubeIframeAPIReady = function() {
    myPlayer = new YT.Player('ytplayer', {
        width: dimensions.width,
        height: dimensions.height,
        videoId: id,
        playerVars : {
            showinfo: 0,
            hl: playerLang,
            autoplay: 1,
            wmode: 'transparent',
            iv_load_policy: 3,
            rel: false
        }
    });
}

Here is a longer code sample, closer to what I'm doing。我尝试将它放在问题中的 sn-p 编辑器中,但是当我尝试运行它时它似乎挂起了我的标签。

当我稍后尝试调用类似:myPlayer.pauseVideo()myPlayer.loadVideoById(options) 时,我得到一个 TypeError 之类的:

TypeError: Object doesn't support property or method 'loadVideoById'

如果我枚举myPlayer 对象的键,则方法不存在。当我测试这些方法时,它们不存在。

我发现我可以成功直接向 YouTube iframe 发送消息:

$('#ytplayer')[0].contentWindow.postMessage(
  '{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*'
);

有没有办法让播放器在 IE9 中正常工作?这似乎是最近的一次破坏,changelog 没有提及。

此外,这是一个受控环境。不,此时我无法从 IE9 和 Win7 切换。浏览器上没有安装扩展。我已经在安装了原版的浏览器和 Browserstack 中确认了这一点。

【问题讨论】:

    标签: javascript internet-explorer youtube youtube-iframe-api


    【解决方案1】:

    您需要收听 YouTubeIframeAPIReady 事件。像这样:

    window.onYouTubeIframeAPIReady =  function() {
      // you can call function create new player here.
    }
    

    youtube api js文件加载后,会调用onYouTubeIframeAPIReady函数。

    【讨论】:

    • 是的,对不起。这在调用创建播放器之前就已经发生了。我将修改我的问题以包含完整的代码示例。
    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多