package { import flash.events.Event; import flash.events.NetStatusEvent; import flash.events.SecurityErrorEvent; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; /** * Handles the basic actions needed to load and playback a video over http. This extends Video, * so may be added to the display list to display video, but also handles all of the handling * of NetConnection and NetStream. Only basic error handling is provided. */ public class graphic_flex_image_effects_10_Flex_VideoLoader extends Video { private var _netConnection:NetConnection; private var _netStream:NetStream; private var _streamPath:String; /** * Constructor. Loads a video if path is provided. * * @param width The display object width. * @param height The display object height. * @param path The path to the video to load. */ public function graphic_flex_image_effects_10_Flex_VideoLoader( width:Number=320, height:Number=240, path:String=null ) { this.width = width; this.height = height; if (path != null) { load(path); } } /** * Creates a new NetStream, attaches the stream data and sets up for stream listeners, * then plays video. */ private function connectStream():void { _netStream = new NetStream(_netConnection); _netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); _netStream.client = this; attachNetStream(_netStream); _netStream.play(_streamPath); } /** * Adds or removes the listener for ENTER_FRAME, which sends progress events. * * @param start True to add event listener, false to remove it. */ private function startProgressEvent(start:Boolean):void { if (start) { addEventListener(Event.ENTER_FRAME, onEnterFrame); } else { removeEventListener(Event.ENTER_FRAME, onEnterFrame); } } /** * Handles the ENTER_FRAME event, sending out RENDER events each frame. * * @param event Event dispatced by this instance. */ private function onEnterFrame(event:Event):void { dispatchEvent(new Event(Event.RENDER)); } /** * Handles the net status events from the NetConnection and NetStream. * This starts and stops the progress RENDER event as well the COMPLETE upon completion. * * @param event Event dispatced by NetConnection and NetStream. */ private function onNetStatus(event:NetStatusEvent):void { switch (event.info.code) { case "NetConnection.Connect.Success": connectStream(); break; case "NetStream.Play.StreamNotFound": trace("Stream not found: " + _streamPath); break; case "NetStream.Play.Start": startProgressEvent(true); break; case "NetStream.Play.Stop": startProgressEvent(false); dispatchEvent(new Event(Event.COMPLETE)); break; } } /** * Handles security errors that can be thrown by NetConnection. * This only traces out error. * * @param event Event dispatced by NetConnection. */ private function onSecurityError(event:SecurityErrorEvent):void { trace(event); } /** * All of these handlers are necessary to handle NetStream events that might fire. * These are expected to be on the object that is used for the client property of NetStream. */ public function onMetaData(info:Object):void {} public function onCuePoint(info:Object):void {} public function onImageData(info:Object):void {} public function onPlayStatus(info:Object):void {} public function onTextData(info:Object):void {} public function onXMPData(info:Object):void {} /** * Attempts to connect to a local NetConnection instance, setting up listeners. * If connection is successful, video will be loaded. * * @param path The path to the video to load. */ public function load(path:String):void { _streamPath = path; _netConnection = new NetConnection(); _netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); _netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); _netConnection.connect(null); } } }