Posted at Feb 21, 2011 1:08:43 PM by Ronald Busky | Share
Google offers a powerful API for developers wishing to integrate Youtube videos with their applications. Despite the usefulness of these functions, exploring Google's documentation leaves you feeling like you've wasted hours wading through a sea of information, only to find that there wasn't a single practical example for you to use. So, here is a good example of the Youtube API without all of the fluff:
The first thing you'll need is a Youtube. For our purposes we'll be using http://www.youtube.com/watch?v=BV2aIRzaJZ4. Once you find the video you'd like to embed, hit the embed button just underneath the Youtube video and copy the given code. For maximum compatibility, we will check the "Use old embed object" box to get a regular <object> to embed, like so:
Now all you need is a function that is called whenever the player is ready:
var myplayer = document.getElementById(playerID);
Finally, you can use the different methods provided by Google to stop, start, play, pause, and do a number of other things to your video. For example, myplayer.stopVideo() will stop your video, while myplayer.playVideo(); will start it again.
Hopefully this introduction to the Youtube API can help you to avoid the endless searching through Google's instructions, and let you get down to writing some creative applications!