Youtube Javascript API play (); doesn't work .. what am I doing wrong?

I play with the Javascript YouTubes API. I installed a test page in my local environment, but the playback function does not work. The video is loading and I can press play on the yt video screen, but my “play” link is not working. Can someone tell me what I am doing wrong?

I follow this: http://code.google.com/apis/youtube/js_api_reference.html

<html>
  <head>
    <script type="text/javascript" src="public/javascripts/swfobject.js"></script> 
  </head>

  <body>

    <div id="ytapiplayer">
      You will need Flash 8 or better to view this content.
    </div>

    <script type="text/javascript">
      var params = { allowScriptAccess: "always" };
      var atts = { id: "myytplayer" };
      swfobject.embedSWF("http://www.youtube.com/v/OQSNhk5ICTI&enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "425", "365", "8", null, null, params, atts);

      function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("myytplayer");
      }

      function play() {
        if (ytplayer) {
          ytplayer.playVideo();
        }
      }
    </script>

    <a href="javascript:void(0);" onclick="play();">Play</a>

  </body>
</html>
+3
source share
2 answers

This will not work on local env. You can download, for example. Express web developer, which includes a tiny web server, and you can run it on a local hosting.

+4
source

div ytapiplayer myytplayer
, onYouTubePlayerReady

 function onYouTubePlayerReady(playerId) {
    //ytplayer = document.getElementById("myytplayer");
    ytplayer = document.getElementById("ytapiplayer");
  }

View the fiddle, ,

0

Source: https://habr.com/ru/post/1784227/


All Articles