Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
I have another question. So if only the url is to be passed to the view, is it best practice to create the loader/netconnection/netstream in the MXML component or the mediator?
public class StreamProxy extends Proxy { /*CONST */ public static const NAME : String = "streamProxy" ; /* protected */ protected var streamList : Dictionary ; /***************************************************************** * PROPERTIES * **************************************************************/ /***************************************************************** * CONSTRUCTOR * **************************************************************/ public function StreamProxy(proxyName:String=null, data:Object=null) { super(proxyName, data); init () ; } private function init () : void { streamList = new Dictionary () ; } /***************************************************************** * FUNCTIONS * **************************************************************/ /** * * getStream * helper function to mange multiple streams * */ protected function getStream (nc : NetConnection, streamName : String ) : ExtendedNetStream { var ns : ExtendedNetStream ; //-- check if NetStreamAlreadyExists if (! streamList.hasOwnProperty( streamName )) { ns = new ExtendedNetStream ( nc ) ; ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError ) ; ns.addEventListener(IOErrorEvent.IO_ERROR, onIOError ) ; ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus ) ; ns.addEventListener( ExtendedNetStreamEvent.META_DATA, onMetaData ) ; ns.addEventListener( ExtendedNetStreamEvent.PLAY_STATUS, onPlayStatus ) ; streamList[ streamName ] = ns ; } else ns = streamList[ streamName ] as ExtendedNetStream ; return ns } /** * * @ playStream * */ public function playStream (nc : NetConnection, streamName : String, seek : Number = 0 ) : ExtendedNetStream { var ns : ExtendedNetStream = getStream( nc , streamName ) ; if ( ns.state == PlayerState.PAUSE_STATE ){ ns.resume(); } else ns.play( streamName ); ns.state = PlayerState.PLAY_STATE return ns ; } /** * * @pauseStream * */ public function pauseStream ( nc :NetConnection, streamName : String ) : ExtendedNetStream { var ns : ExtendedNetStream = getStream( nc , streamName ) ; ns.pause(); ns.state = PlayerState.PAUSE_STATE; return ns ; } /** * * @stopStream * */ public function stopStream ( nc :NetConnection, streamName : String ) : ExtendedNetStream { var ns : ExtendedNetStream = getStream( nc , streamName ) ; ns.close(); ns.state = PlayerState.STOP_STATE; return ns ; } /** * * @seek * */ public function seek ( nc :NetConnection, streamName : String , position : Number ) : ExtendedNetStream { var ns : ExtendedNetStream = getStream( nc , streamName ) ; ns.seek( position ) ; return ns ; } /** * * @ getSeek * get the current seek position of a * selected netStream * */ public function getSeek ( streamName : String) : Number { var ns :ExtendedNetStream = streamList[ streamName ] as ExtendedNetStream ; if (! ns ) return -1 ; return ns.time / ns.duration *100 ; } /***************************************************************** * EVENTS * **************************************************************/ private function onAsyncError ( e: AsyncErrorEvent ) : void { facade.sendNotification( MainFacade.NS_ASYNC_ERROR ,e ) ; } private function onIOError ( e : IOErrorEvent ) : void { facade.sendNotification( MainFacade.NS_IO_ERROR, e ) ; } private function onNetStatus ( e : NetStatusEvent ) : void { trace ("[ StreamProxy ]", e.target, e.info.code ); switch ( e.info.code ){ case "NetStream.Play.Stop" : // TODO : send to interface state == STOP break ; } } /** * * onMetaData * */ private function onMetaData ( e : ExtendedNetStreamEvent ) : void { try { //-- save in a dynaimc var the (e.target as ExtendedNetStream ).duration = (e.info as Array)["duration"]; }catch (err : Error){} } /** * * @ onPlayStatus * */ private function onPlayStatus ( e : ExtendedNetStreamEvent ): void { trace (e.info.code) ; switch (e.info.code) { case "NetStream.Play.Complete": (e.target as ExtendedNetStream).close() ; facade.sendNotification( MainFacade.NOTIFY_PLAY_COMPLETE, e.target as NetStream) ; break ; } }