YouTube iframe API - onReady 和 onStateChanged 事件未在 IE9 中触发 [英] YouTube iframe API - onReady and onStateChanged events not firing in IE9

查看:24
本文介绍了YouTube iframe API - onReady 和 onStateChanged 事件未在 IE9 中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数小时以来,我一直在进行故障排除并寻找答案,但无济于事.正在逐字使用 API 文档中的示例代码.其他人在 IE9 上发布和说的示例代码不适合我.我已经确认在 html 中正确设置了 doctype,我没有在 CSS 中使用 display=none 隐藏 iframe,浏览器模式是 IE9,文档模式是 IE9 标准.我的安全设置被配置为中高.在我的 IE9 浏览器中,即使是 google 的示例页面也不适合我.

I have been troubleshooting and searching for an answer to this for hours, to no avail. Sample code from the API documentation is being used verbatim. Sample code that others post and say works on IE9 doesn't for me. I've already confirmed the doctype is properly set in the html, I'm NOT hiding the iframe using display=none in the CSS, and the browser mode is IE9 and document mode is IE9 standards. My security settings are configured to medium high. Even google's sample page does not work for me in my IE9 browser.

https://developers.google.com/youtube/youtube_player_demo

当我点击播放"尝试播放电影时(在上面的链接中),我在 IE 的脚本调试控制台中收到各种错误.第一个错误如下所示:

When I click "play" to try to make the movie play (in the above link), I get all kinds of errors in the script debugging console for IE. First error looks like this:

SCRIPT5009:未定义视频"

SCRIPT5009: 'video' is undefined

请帮忙!我的示例代码如下.电影加载,我看到API 准备就绪"警报,但其他事件都不会在 IE9 中触发.我只是不知道我错过了什么!如果我不尽快解决这个问题,我将不得不放弃在我的项目中使用 YouTube 播放 html5 视频.

Please help! My sample code is below. The movie loads and I see the "API is ready" alert but none of the other events will fire in IE9. I just don't know what I am missing! If I don't figure this out soon I'm going to have to abandon the use of YouTube for html5 video playback in my project.

<!DOCTYPE html>
<html>
  <head><title></title></head>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script type="text/javascript">
        // 2. This code loads the IFrame Player API code asynchronously.
        var tag = document.createElement('script');

        // This is a protocol-relative URL as described here:
        //     http://paulirish.com/2010/the-protocol-relative-url/
        // If you're testing a local page accessed via a file:/// URL, please set tag.src to
        //     "https://www.youtube.com/iframe_api" instead.
        //tag.src = "//www.youtube.com/iframe_api";
        tag.src = "https://www.youtube.com/iframe_api";
        //tag.src = "http://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // 3. This function creates an <iframe> (and YouTube player)
        //    after the API code downloads.
        var player;
        function onYouTubeIframeAPIReady() {
            alert("API is ready!");
            player = new YT.Player('player', {
                height: '450',
                width: '800',
                videoId: 'mU7aJgvh9K8',
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
        }

        // 4. The API will call this function when the video player is ready.
        function onPlayerReady(event) {
            event.target.playVideo();
            alert("Player is ready!");
        }

        // 5. The API calls this function when the player's state changes.
        //    The function indicates that when playing a video (state=1),
        //    the player should play for six seconds and then stop.
        var done = false;
        function onPlayerStateChange(event) {
            alert("State changed!");
            if (event.data == YT.PlayerState.PLAYING && !done) {
                setTimeout(stopVideo, 6000);
                done = true;
            }
        }
        function stopVideo() {
            player.stopVideo();
        }
    </script>
  </body>
</html>

推荐答案

好吧,在同事的机器上测试并意识到问题是特定于我系统上的 IE 后,我再次开始查看浏览器设置.我一时兴起禁用了我的 Flash 播放器(在 Internet 选项、程序、管理加载项下)和中提琴,视频代码就像一个魅力.我还注意到我的 Flash 播放器已经过时 (9.0.45.0),并且在安装最新版本的播放器后它仍然可以工作.

Well, after testing on a colleague's machine and realizing the problem was specific to IE on MY system, I started looking at the browser settings again. On a whim I disabled my Flash player (under Internet Options, Programs, Manage Add-Ons) and viola, the video code worked like a charm. I also noticed my Flash player was outdated (9.0.45.0) and after installing the latest version of the player it still worked.

不知道为什么旧的 Flash 播放器会干扰 YouTube API 和事件 - 但显然这是罪魁祸首.

No idea why the old Flash player was interfering with the YouTube API and events - but apparently that was the culprit.

这篇关于YouTube iframe API - onReady 和 onStateChanged 事件未在 IE9 中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆