2013-03-05 88 views
3

我一直在進行故障診斷並搜索了幾個小時的答案,但無濟於事。 API文檔中的代碼示例正在逐字使用。其他人發佈和說在IE9上工作的示例代碼不適合我。我已經確認了doctype在html中正確設置,我沒有在CSS中使用display = none隱藏iframe,瀏覽器模式是IE9,文檔模式是IE9標準。我的安全設置配置爲中等偏高。即使在我的IE9瀏覽器中,谷歌的示例頁面也不適用於我。YouTube iframe API - onReady和onStateChanged事件在IE9中未觸發

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

當我點擊「播放」,力圖使影片播放(在上面的鏈接),我得到的各種錯誤的腳本調試IE控制檯。第一個錯誤是這樣的:

SCRIPT5009: '視頻' 是不確定的

請幫幫忙!我的示例代碼如下。電影加載後,我看到「API已準備就緒」警報,但其他事件都不會在IE9中觸發。我只是不知道我錯過了什麼!如果我不能很快了解這一點,我將不得不放棄在我的項目中使用YouTube進行html5視頻播放。

<!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> 

回答

8

那麼,在一個同事的機器上測試並發現問題是特定於我的系統上的IE瀏覽器後,我開始再次查看瀏覽器設置。一時興起,我禁用了我的Flash播放器(在Internet選項,程序,管理附加組件)和中提琴上,視頻代碼像魅力一樣工作。我還注意到我的Flash播放器已經過時(9.0.45.0),並且在安裝最新版本的播放器之後它仍然工作。

不知道爲什麼舊的Flash播放器干擾YouTube API和事件 - 但顯然這是罪魁禍首。

+2

我沒有足夠的聲望投票或評論,但我想說謝謝,因爲這解決了我一整天的頭痛!如果我找到讓他們一起工作的方法,我會回覆。編輯:如果它幫助別人;我們通過使用定位來顯示和隱藏YouTube容器而不是使用「display:none」或「visibility:hidden」來解決此問題。 – Wayferer 2013-04-10 00:03:46

+0

關於甚至不使用'visibility:hidden'來代替'display:none'的部分是我的優點。謝謝。 – 2014-03-05 14:39:24

相關問題