YouTube API — not firing 'onYouTubePlayerReady()'

Ashley Williams picture Ashley Williams · Jan 18, 2010 · Viewed 20.2k times · Source

From what I've read, this is how I should setup the YouTube API:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta content='text/html;charset=UTF-8' http-equiv='content-type' />
    <title>Youtube Player</title>
    <script src="jquery.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8">
      function onYouTubePlayerReady(id) {
        console.log("onYouTubePlayerReady() Fired!");
        var player = $("#youtube_player").get(0);
      }

      var params = { allowScriptAccess: "always" };
      var atts = { id: "youtube_player" };
      swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1", 
                         "youtube", "425", "356", "8", null, null, params, atts);

    </script>
  </head>
  <body>
    <div id="youtube"></div>
  </body>
</html>

However, 'onYouTubePlayerReady()' doesn't fire at all, and if I manually get a reference to the player, a lot of methods are undefined; for example, cueVideoById() works, but playVideo() doesn't.

How can I fix this problem?

Answer

Pekka picture Pekka · Jan 18, 2010

You need to be on a web server with your test script, as stated in the documentation:

Note: To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet.