2009-07-03 3 views
1

Код выглядит следующим образом, но я получаю ошибку «ytplayer is not defined». Является ли это проблемой с«ytplayer не определен» при использовании метода SWFObject для включения видеохроменного видео Youtube на сайт

<script src="http://www.google.com/jsapi"></script> 
<script> 
    google.load("swfobject", "2.1"); 
</script> 

<script type="text/javascript"> 

     function updateHTML(elmId, value) { 
      document.getElementById(elmId).innerHTML = value; 
     } 

     function setytplayerState(newState) { 
      updateHTML("playerstate", newState); 
     } 

     function onYouTubePlayerReady(playerId) { 
      ytplayer = document.getElementById("myytplayer"); 
      ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); 
      ytplayer.addEventListener("onError", "onPlayerError"); 
     } 

     function onytplayerStateChange(newState) { 
      setytplayerState(newState); 
     } 

     function onPlayerError(errorCode) { 
      alert("An error occured: " + errorCode); 
     } 

     // functions for the api calls 
     function loadNewVideo(id, startSeconds) { 
      if (ytplayer) { 
      ytplayer.loadVideoById(id, parseInt(startSeconds)); 
      } 
     } 

     function play() { 
      if (ytplayer) { 
      ytplayer.playVideo(); 
      } 
     } 


    </script> 

и его помещенной здесь

<div id="ytapiplayer"> 

</div> 
<script type="text/javascript"> 
     // <![CDATA[ 

     // allowScriptAccess must be set to allow the Javascript from one 
     // domain to access the swf on the youtube domain 
     var params = { allowScriptAccess: "always", wmode: "transparent", menu: "false" }; 
     // this sets the id of the object or embed tag to 'myytplayer'. 
     // You then use this id to access the swf and make calls to the player's API 
     var atts = { id: "myytplayer" }; 
     swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", 
         "ytapiplayer", "100%", "100%", "8", null, null, params, atts); 
     loadNewVideo('11mrMppgfrQ','0'); 
     play(); 
//]]> 
    </script> 

onYouTubePlayerReady работы и alert(ytplayer); в этой функции возвращает объект, однако вызова alert(ytplayer); в loadNewVideo возвращает нуль.

Это копия непосредственно из http://code.google.com/apis/youtube/chromeless_example_1.html, где все работает отлично. Любые идеи, в которых я ошибаюсь? Это связано с вызовом getElementById для объекта?

+0

Извините, удаленный обман – ewengcameron

ответ

1

Обнаружена проблема.

loadNewVideo('11mrMppgfrQ','0'); 
     play(); 

называли до того

function onYouTubePlayerReady(playerId) { 
     ytplayer = document.getElementById("myytplayer"); 
     ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); 
     ytplayer.addEventListener("onError", "onPlayerError"); 
    } 

Перемещение их в функции после этой строки

ytplayer = document.getElementById("myytplayer"); 

исправили проблему

окончательный код выглядит следующим образом:

function onYouTubePlayerReady(playerId) { 
    ytplayer = document.getElementById("myytplayer"); 
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); 
    ytplayer.addEventListener("onError", "onPlayerError"); 
    loadNewVideo('11mrMppgfrQ','0'); 
    play(); 
}