Adding some error checking

If there’s 1 thing I learned from OOAD folks, it’s this: avoid writing “The Kitchen Sink” class. In other words, make sure your classes don’t try to do everything at once. Some purists demand that there be 1 task per class. Others are OK with 2 – 3 tasks per class. In this AS3 video example, I separated the Error Checking task and the Meta Data processing task into their own separate classes. This helped reduce size & length of the Main class and parceled the Error Checking and Meta Data functionality out for easier re-use in other Flash / Flex video projects.  

Main.as, the client / driver / document class:

package
{
	import utls.ErrorChecker;
	import vid.CustomClient;
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import flash.events.NetStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.display.Sprite;
	import flash.media.Video;

	[SWF (width="600", height="400", backgroundColor="#9999cc", frameRate="30")]
    public class Main extends Sprite
	{
		private var ec:ErrorChecker;
		private var nc:NetConnection;
		private var ns:NetStream;
		private var theUrl: String = "http://www.helpexamples.com/flash/video/cuepoints.flv";

		public function Main()
		{
			nc = new NetConnection();
			nc.connect(null);

			ns = new NetStream(nc);
			ns.bufferTime = 10;
			ns.client = new CustomClient();
			ns.receiveAudio(true);
			ns.receiveVideo(true);
			ns.play(theUrl);

			ec = new ErrorChecker();

			// check for errors on the NetConnection
			nc.addEventListener(NetStatusEvent.NET_STATUS, ec.doNetStatus);
			nc.addEventListener(IOErrorEvent.IO_ERROR, ec.doIOError);
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ec.doSecurityError);

			// check for errors on the NetStream
			ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ec.doAsyncError);
			ns.addEventListener(NetStatusEvent.NET_STATUS, ec.doNetStatus);
			ns.addEventListener(IOErrorEvent.IO_ERROR, ec.doIOError);

			var vid:Video = new Video();
			vid.attachNetStream(ns);
			addChild(vid);
		}
	}
}

the ErrorChecker Class:

package utls
{
	import flash.events.NetStatusEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;

    public class ErrorChecker
	{
		public function ErrorChecker() { }

		public function doSecurityError(e:SecurityErrorEvent):void
		{
			// crossdomain xml file should fix this error
                        trace("AbstractStream.securityError:"+e.text);
		}

		public function doIOError(e:IOErrorEvent):void
		{
			trace("AbstractScreem.ioError:"+e.text);
		}

		public function doAsyncError(e:AsyncErrorEvent)
		{
			trace("AsyncError:"+e.text);
		}

		public function doNetStatus(e:NetStatusEvent):void
		{
			trace(e.info.code);
		}

    }
}

the CustomClient class:

package vid
{
	import flash.events.NetStatusEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;

    public class CustomClient
	{

		private var cuePointCount:int = 0;

        public function onMetaData(infoObject:Object):void
		{
            trace("metadata");
			trace("duration =" + infoObject.duration);
			trace("width =" + infoObject.width);
			trace("height =" + infoObject.height);
			trace("framerate =" + infoObject.framerate + "\n");
        }

		public function onCuePoint(infoObject:Object):void
		{
			cuePointCount += 1;
			switch(cuePointCount)
			{ //the flv has 3 cue points created @ encoding time
				case 1:
					trace("cue point VONE!");
					cuePntDetails(infoObject);
					break;
				case 2:
					trace("cue point TWO!");
					cuePntDetails(infoObject);
					break;
				case 3:
					trace("cue point THREEE!");
					cuePntDetails(infoObject);
					break;
			}
		}

                //can function as an onComplete only for streaming, not progressive download
		private function onPlayStatus(infoObject:Object):void
		{ //only works with Flash Media Server / streaming video, not progressive download
			switch (infoObject.info.code)
			{
				case "NetStream.Play.Complete":
					vidComplete();
					break;
			}
			trace(infoObject.info.code);
		}

		public function cuePntDetails(infoObject):void
		{
			trace("name: " + infoObject.name);
			trace("time: " + infoObject.time);
			trace("type: " + infoObject.type); //navigation or event
			trace("params: " + infoObject.parameters + "\n");
		}

		public function vidComplete():void
		{
			trace("NetStream.Play.Complete = Video complete");
		}

    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s