最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Youtube Javascript API play(); not working.. what am I doing wrong? - Stack Overflow

matteradmin7PV0评论

I'm playing around with YouTubes Javascript API. I've set up a test page on my local enviornment, but the play function is not working. The video loads, and I can press play on the yt video screen, but my link "play" isn't working. Can someone tell me what I'm doing wrong??

I've been following this: .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(";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>

I'm playing around with YouTubes Javascript API. I've set up a test page on my local enviornment, but the play function is not working. The video loads, and I can press play on the yt video screen, but my link "play" isn't working. Can someone tell me what I'm doing wrong??

I've been following this: http://code.google./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./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>
Share Improve this question asked Jan 8, 2011 at 8:22 thedeepfieldthedeepfield 6,20625 gold badges74 silver badges108 bronze badges 1
  • Your exact code worked just fine for me on IE, Chrome and Firefox. If you're using one of those add such debugging: function play() { alert(typeof ytplayer); if (ytplayer) { alert(ytplayer.playVideo); ytplayer.playVideo(); } } what alerts do you see? – user447356 Commented Jan 8, 2011 at 8:44
Add a ment  | 

2 Answers 2

Reset to default 3

That won't work on a local env. You can download e.g. web developer express which has included a tiny web server, and you can run it in localhost.

Your div id is ytapiplayer not myytplayer
So, change the function onYouTubePlayerReady as

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

View the fiddle to see the code working

Post a comment

comment list (0)

  1. No comments so far