package thecouch { import flash.display.Loader; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLRequest; import flash.system.Security; /** * ... * @author Peter Backx - streamhead.com */ public class YouTubePlayer extends Loader { public static var UNSTARTED:String = "-1"; public static var ENDED:String = "0"; public static var PLAYING:String = "1"; public static var PAUSED:String = "2"; public static var BUFFERING:String = "3"; public static var CUED:String = "5"; public static var PLAYER_READY:String = "YT_player_ready"; public function YouTubePlayer() { Security.allowDomain("http://www.youtube.com"); contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit); load(new URLRequest("http://www.youtube.com/apiplayer?version=3")); } private function onLoaderInit(event:Event):void { content.addEventListener("onReady", onPlayerReady); content.addEventListener("onError", onPlayerError); content.addEventListener("onStateChange", onPlayerStateChange); content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange); } private function onPlayerStateChange(event:Event):void { // Event.data contains the event parameter, which is the new player state var eventCode:String = Object(event).data; if (eventCode == ENDED) { dispatchEvent(new Event(ENDED)); } } private 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. Object(content).setSize(320,240); dispatchEvent(new Event(PLAYER_READY)); } private function onPlayerError(event:Event):void { // Event.data contains the event parameter, which is the error code // This event is fired when an error in the player occurs. The possible error codes are 100, 101, and 150. The 100 error code is broadcast when the video requested is not found. This occurs when a video has been removed (for any reason), or it has been marked as private. The 101 error code is broadcast when the video requested does not allow playback in the embedded players. The error code 150 is the same as 101, it's just 101 in disguise! trace("player error:", Object(event).data); } private function onVideoPlaybackQualityChange(event:Event):void { // Event.data contains the event parameter, which is the new video quality trace("video quality:", Object(event).data); } public function loadVideoById(id:String):Object { return Object(content).loadVideoById(id); } public function loadVideoByUrl(url:String):Object { return Object(content).loadVideoByUrl(url); } } }