How to set a YouTube video to Autoplay

Written by on April 13, 2010 in Coding - 2 Comments
youtube-logo

So you know how to embed a YouTube video onto your site right? Well good for you! Copy and paste the code YouTube gives you, easy enough. But what if you want to set the video to autoplay? What I mean by that is have it start playing as soon as the user opens your webpage. By default the user would need to click Play on the video to start the play cycle. It’s easy enough to set the video to autoplay however, check out the demo and keep reading for the code.

View Demo

So all you need to do to set the video to autoplay is add the following:

&autoplay=1

Right at the end of the two URLs in the embed code that you copied from Youtube. So in the demo above, this was the original code:

<object width=”480″ height=”385″><param name=”movie” value=”http://www.youtube.com/v/SlTvSUCCqPo&hl=en_US&fs=1&rel=0″></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/SlTvSUCCqPo&hl=en_US&fs=1&rel=0″ type=”application/x-shockwave-flash” allowscriptaccess=”always” allowfullscreen=”true” width=”480″ height=”385″></embed></object>

After I added the above code to the end of the URLs the embed code looked like this:

<object width=”480″ height=”385″><param name=”movie” value=”http://www.youtube.com/v/SlTvSUCCqPo&hl=en_US&fs=1&rel=0&autoplay=1″></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/SlTvSUCCqPo&hl=en_US&fs=1&rel=0&autoplay=1″ type=”application/x-shockwave-flash” allowscriptaccess=”always” allowfullscreen=”true” width=”480″ height=”385″></embed></object>

And now the video will autoplay as soon as somebody opens the webpage.

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Check him out on Twitter, Facebook, Google+ and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.

  • http://www.conceitodigital.com Paulo

    Very nice… good post ;)

  • http://www.avirtualedge.com Hilary

    Thanks for sharing! I had always been wondering how to do this and came across your post now that a client has made the request for one of their videos to autoplay.

    Thanks again!