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
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!
Hey nice work. Will definitely be looking at this at some point 🙂
Very nice. Good little tutorial on how to use the youtube api.
When I put a feed url for a certain playlist the list populates but the video doesn’t play. any suggestions?
http://gdata.youtube.com/feeds/api/playlists/320BAFE81584B804?v=2
Try this onLoad function:
This code works! Congratulations!
How can I retrieve the thumbnail of the video?
This is how you can retrieve the thumbnail: “http://img.youtube.com/vi/” + videoID + “/2.jpg”
can you alter this to show a playlist… if possible… HOW?? futhermore AWESOME
Actually you can, yes.
I did that for a player of mine that I sell on ActiveDen:
http://activeden.net/item/youtube-playlist-reader/97090?ref=vamapaull