« Transparent reflection class for Bitmaps | Home | Why do i think diggbar is not ready yet »

March 14, 2009

NetStream data callbacks as events

Today i saw that Jesse Warden was reporting via twitter that he found some really strange behavior on the client property of the a NetStream. After a couple of tweets i mentioned that i had a custom NetStream class which converts those data callbacks into real events (read: addEventListener). He found the idea interesting so here it is.

First of all we need a custom event class in order to proxy the Object argument received on the callbacks:

package inca.events { import flash.events.Event; public class DataEvent extends Event { private var $__data:Object; public function DataEvent(type:String, data:Object){ super(type); $__data = data; } public function get data():Object{ return $__data; } override public function clone():Event{ return new DataEvent(type, $__data); } } }

It's a generic class that i use for several other things.

And here is the custo NetStream class:

package inca.net { import flash.events.Event; import flash.net.NetConnection; import inca.events.DataEvent; public class NetStream extends flash.net.NetStream { public static const METADATA:String = "metadata"; public static const XMP_DATA:String = "xmp_data"; public static const CUE_POINT:String = "cue_point"; public static const IMAGE_DATA:String = "image_data"; public static const TEXT_DATA:String = "text_data"; public static const DRM_CONTENT:String = "drm_content"; public static const PLAY_STATUS:String = "play_status"; public function NetStream(connection:NetConnection, peerID:String=CONNECT_TO_FMS){ super(connection, peerID); client = { onXMPData: function(info:Object):void{ dispatchEvent(new DataEvent(XMP_DATA, info)); }, onMetadata: function(info:Object):void{ dispatchEvent(new DataEvent(METADATA, info)); }, onCuePoint: function(info:Object):void{ dispatchEvent(new DataEvent(CUE_POINT, info)); }, onImageData: function(info:Object):void{ dispatchEvent(new DataEvent(IMAGE_DATA, info)); }, onTextData: function(info:Object):void{ dispatchEvent(new DataEvent(TEXT_DATA, info)); }, onDRMContentData: function(info:Object):void{ dispatchEvent(new DataEvent(DRM_CONTENT, info)); }, onPlayStatus: function(info:Object):void{ dispatchEvent(new DataEvent(PLAY_STATUS, info)); } }; } } }

I use the name class name in order to avoid using flash's NetStream at all on my projects.

If it is not working correctly don't panic, i'll fix it later as soon as i can get flex builder to open on this laptop so i can verify that i didn't remove smth important on that class (that class has a lot more stuff that i just don't feel like sharing publicly yet).

--fernando

5 Comments

Just use Proxy extending class as NetStream client for it:

override flash_proxy function callProperty(name, ...args):*{
owner.dispatchEvent(new VideoStreamEvent(VideoStreamEvent.UNKNOWN_CALL, name, args));
return null;
}

where VideoStreamEvent is:
VideoStreamEvent(type, callbackName, callbackArgs)

This is great, Fernando. Thanks for sharing!

That's actually a good idea. I posted some oddities regarding NetStream not long ago:

http://blog.zarate.tv/2009/02/20/netstream-ioerrorevent-urlrequest-and-other-woes/

It might be a good idea going for something similar to what you did.

Cheers!

Juan

hi, maliboo

Can you please give a bit more details of your solution?

@smith: you delegate all calls to NetStream.client.method to higher level with custom event dispatching from NetStream. Every call to undefined method fires event dispatching mechanism with callback name and its arguments. See flash.utils.Proxy class, or __resolve method in AS2. I've also add most popular methods, like onMetaData and onCuePoint directly as:

public function onMetaData(_arg1:Object):void{
owner.dispatchEvent(new VideoStreamEvent(VideoStreamEvent.META_DATA, _arg1));
}

public function onCuePoint(_arg1:Object):void{
owner.dispatchEvent(new VideoStreamEvent(VideoStreamEvent.CUE_POINT, _arg1));
}

About this Entry

This page contains a single entry by fernando published on March 14, 2009 2:50 PM.

Transparent reflection class for Bitmaps was the previous entry in this blog.

Why do i think diggbar is not ready yet is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.