A few days ago I made a little application based on the new ActionScript 3 YouTube API and I thought I should share it with those who want to get started with this type of players. In the future I plan to make some more complex applications based on this API so I can sell them on ActiveDen.net



Here you can see the code:

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
 
Security.allowDomain("youtube.com","http://youtube.com","www.youtube.com");
 
var feedName:String = "MyDamnChannel";
 
// This will hold the API player instance once it is initialized.
var player:Object;
 
var playerLoader:Loader = new Loader();
playerLoader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
playerLoader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
 
function onLoaderInit(event:Event):void {
	addChild(playerLoader);
	playerLoader.content.addEventListener("onReady", onPlayerReady);
	playerLoader.content.addEventListener("onError", onPlayerError);
	playerLoader.content.addEventListener("onStateChange", onPlayerStateChange);
	playerLoader.content.addEventListener("onPlaybackQualityChange", 
		onVideoPlaybackQualityChange);
}
 
function onPlayerReady(event:Event):void {
	// Event.data contains the event parameter, which is the Player API ID 
	//trace("player ready:", Object(event).data);
 
	// Once this event has been dispatched by the player, we can use
	// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
	// to load a particular YouTube video.
	player = playerLoader.content;
	player.x = 300;
	player.y = 30;
	player.setSize(580,345);
}
 
function onPlayerError(event:Event):void {
	// Event.data contains the event parameter, which is the error code
	//trace("player error:", Object(event).data);
}
 
function onPlayerStateChange(event:Event):void {
	// Event.data contains the event parameter, which is the new player state
	//trace("player state:", Object(event).data);
}
 
function onVideoPlaybackQualityChange(event:Event):void {
	// Event.data contains the event parameter, which is the new video quality
	//trace("video quality:", Object(event).data);
}
 
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
 
lb.addEventListener(Event.CHANGE, itemChange);
 
function itemChange(e:Event):void
{
	player.loadVideoById(lb.selectedItem.data,0);
}
 
var xml:XML;
 
function onLoaded(e:Event):void
{
 
	var defaultNS:Namespace = new Namespace("http://www.w3.org/2005/Atom");
 
	xml = new XML(e.target.data);
	var il:XMLList = xml.defaultNS::entry;
	for(var i:uint=0; i<il.length(); i++)
	{
 
		var ytId:String = il[i].defaultNS::id.slice(42,53);
		lb.addItem({data:ytId, label:il[i].defaultNS::title});
	}
}
 
loader.load(new URLRequest("http://gdata.youtube.com/feeds/api/users/"+feedName+"/uploads"));

Download FLA:

  ActionScript 3 YouTube feed application (607.8 KiB, 3,101 hits)

Enjoy!