YouTube ActionScript 3.0 Player, Welcome to the ActionScript future

This weekend, I finally brought The Couch TVs frontpage up to date. It had been broken for a while, ever since my little YouTube ActionScript 3 hack stopped working. Since YouTube announced their ActionScript 3.0 player, a few months ago, there is no need for a hack anyway. Now there is a clean way to integrate YouTube in your ActionScript 3 programs.

Integration is incredibly easy, you can read the documentation in about 10 minutes and that’s really all you need to know. No hidden options or special URLs necessary.

I did create a small wrapper class, to make addressing the player a little more typesafe (and this also gives you autocompletion). Check out my Assembla SVN repository for the full code of what you can see on The Couch TV (there’s code for full screen in there that doesn’t work right now).

You can find the wrapper right here. The YouTubePlayer class extends Loader, which means you can add it anywhere you like. The class first needs to load the actual YouTube player on YouTube.com. This means you need to wait until everything is loaded before you can use the player. In ActionScript code it looks like this:

package thecouch 
{
	import flash.display.Sprite;
	import flash.events.Event;
	/**
	 * @author Peter Backx - thecouchtv.com
	 */
	public class SimpleExample extends Sprite 
	{
		private var player:YouTubePlayer;
		
		public function SimpleExample() 
		{
	        addChild(player = new YouTubePlayer());
			player.addEventListener(YouTubePlayer.PLAYER_READY, playerReady);
		}
		
		private function playerReady(event:Event):void 
		{
			player.loadVideoById("2gGopKNPqVk");
		}
	}
	
}

I’ve also added wrappers for the loadVideoByUrl methods and a listener for the “ended” even. That means, you can use those directly on the YouTubePlayer object. You’ll have nice code completion in your editor. And unlike the example in the documentation this means you also have compile time type safety. The joy of typesafe object oriented programming.

The Main class shows you how to use them. If you want other methods or events, feel free to add them, or you can always ask me. I’d be delighted to expand this wrapper a little more.

The previous posts in this series (not all of them are still applicable, but will be updated shortly):

Next up, I’m going to remove the proprietary YouTube Data API calls with this project.