topical media & game development

talk show tell print

#graphic-flex-image-effects-10-Flex-VideoLoader.ax

#graphic-flex-image-effects-10-Flex-VideoLoader.ax [swf] [flash] flex


  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 @ax-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.
parameter: width The display object width.
parameter: height The display object height.
parameter: path The path to the video to load.

  
                  public function @ax-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.
parameter: 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.
parameter: 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.
parameter: 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.
parameter: 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.
parameter: 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);
                  }
  
          }
  
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.