DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Randall has posted 3 posts at DZone. View Full User Profile

Autoplay YouTube SWF Using API, Not Autoplay=1

01.01.2009
| 10944 views |
  • submit to reddit
        You need to

1. Download SWFobject, not link to YouTube's - http://code.google.com/p/swfobject/

2. Change swfobject.embedSWF("http://www.youtube.com/v/OdqhDcAoQuc to the URL of your video

the setTimeout is very important, it will not work without it - but you may be able to set the timer to less than 5000 milliseconds

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
      YouTube Javascript API Autostart
    </title>
    <script src="swfobject.js" type="text/javascript"></script>


    <script type="text/javascript">
setTimeout('myytplayer.playVideo()',5000);
    </script>
  </head>
  <body>
  <div>
    <div id="ytapiplayer">
      You need Flash player 8+ and JavaScript enabled to view this video.
    </div>
    <script type="text/javascript">
      // allowScriptAccess must be set to allow the Javascript from one domain to access the swf on the youtube domain
      var params = { allowScriptAccess: "always" };
     // 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/v/OdqhDcAoQuc&border=0&enablejsapi=1&playerapiid=ytplayer", 
                         "ytapiplayer", "425", "344", "8", null, null, params, atts);
    </script>

  </body>
</html>