Flash/Flex 4 NetConnection onbject must be connected

Hello Expets,
I am having a little trouble in connecting to an RMTP stream using netstream within Flashbuilder 4.

i have used sample code to come up with this.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/14/creating-timers-using-the-setinterval-method/ -->
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
                                    layout="vertical"
                                    verticalAlign="middle"
                                    backgroundColor="white"
                                  creationComplete="init();">

      <mx:Script>
            <![CDATA[
                  import mx.utils.ObjectUtil;
                                    
                  private var nc:NetConnection;
                  private var ns:NetStream;
                  private var video:Video;
                  private var meta:Object;
                  
                  private function init():void {
                  var nsClient:Object = {};
                              
                  nsClient.onMetaData = ns_onMetaData;
                  nsClient.onCuePoint = ns_onCuePoint;
                  
                  nc = new NetConnection();
                  nc.connect("rtmp://vhs-Red5/oflaDemo/");
                                    
                  ns = new NetStream(nc);
                  ns.client = nsClient;
                  ns.play("avatar");
                  
                  video = new Video();
                  video.attachNetStream(ns);
                  uic.addChild(video);
                  
                  }

                  private function ns_onMetaData(item:Object):void {
                        trace("meta");
                        meta = item;
                        // Resize Video object to same size as meta data.
                        video.width = item.width;
                        video.height = item.height;
                        // Resize UIComponent to same size as Video object.
                        uic.width = video.width;
                        uic.height = video.height;
                        panel.title = "framerate: " + item.framerate;
                        panel.visible = true;
                        trace(ObjectUtil.toString(item));
                  }
                  private function ns_onCuePoint(item:Object):void {
                        trace("cue");

                  }
            ]]>
      </mx:Script>
      
      <mx:Panel id="panel" visible="true">
            <mx:UIComponent id="uic" />
            <mx:ControlBar>
                  <mx:Button label="Play/Pause" click="ns.togglePause();" />
                  <mx:Button label="Rewind" click="ns.seek(0); ns.pause();" />
                  </mx:ControlBar>
      </mx:Panel>
      
</mx:WindowedApplication>

However when i run the application i am  given in the arugment error NetConnection object must be connected.

Any ideas?

Thanks in Advance
LVL 2
Dave_Angel_PortsmouthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rupvisCommented:
at nc= new NetConnection();

try this..

  NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
            nc = new NetConnection();
            nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
            nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            nc.connect("rtmp://vhs-Red5/oflaDemo/");

Just make sure you import all the additional classes..

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave_Angel_PortsmouthAuthor Commented:
import the additional classes?

Sorry if i am asking a silly question very new to flex
0
rupvisCommented:
Yes the above code might give your a compilation error for flash.
So you might need to add the following imports below  import mx.utils.ObjectUtil;in your code
import flash.net;
import flash.display.Sprite;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.NetStatusEvent;

 
0
rupvisCommented:
My APologies i forgot to add the handler function

 private function netStatusHandler(event:NetStatusEvent):void {
            trace("connected is: " + nc.connected);
            trace("event.info.level: " + event.info.level);
            trace("event.info.code: " + event.info.code);
        }
0
Dave_Angel_PortsmouthAuthor Commented:
thank you.

when adding the import for flash.net i was presented with " Definition flash.net could not be found"
upon removing that one line the error has gone and i am able to run the program however i am still getting the "NetConnection must be connected error"

code is currently:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
                                    layout="vertical"
                                    verticalAlign="middle"
                                    backgroundColor="white"
                                  creationComplete="init();">

      <mx:Script>
            <![CDATA[
                import mx.utils.ObjectUtil;
                  import flash.display.Sprite;
                  import flash.net.NetConnection;
                  import flash.net.NetStream;
                  import flash.events.NetStatusEvent;

                  private function netStatusHandler(event:NetStatusEvent):void {
                        trace("connected is: " + nc.connected);
                        trace("event.info.level: " + event.info.level);
                        trace("event.info.code: " + event.info.code);
                  }
                                    
                  private var nc:NetConnection;
                  private var ns:NetStream;
                  private var video:Video;
                  private var meta:Object;
                  
                  private function init():void {
                  var nsClient:Object = {};
                                                                  
                  nsClient.onMetaData = ns_onMetaData;
                  nsClient.onCuePoint = ns_onCuePoint;
                  NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
                  nc = new NetConnection();
                  nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
                  nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
                  nc.connect("rtmp://vhs-Red5/oflaDemo/");

                                    
                  ns = new NetStream(nc);
                  ns.client = nsClient;
                  ns.play("avatar");
                  video = new Video();
                  video.attachNetStream(ns);
                  uic.addChild(video);
                  
                  }

                  private function ns_onMetaData(item:Object):void {
                        trace("meta");
                        meta = item;
                        // Resize Video object to same size as meta data.
                        video.width = item.width;
                        video.height = item.height;
                        // Resize UIComponent to same size as Video object.
                        uic.width = video.width;
                        uic.height = video.height;
                        panel.title = "framerate: " + item.framerate;
                        panel.visible = true;
                        trace(ObjectUtil.toString(item));
                  }
                  private function ns_onCuePoint(item:Object):void {
                        trace("cue");

                  }
            ]]>
      </mx:Script>
      
      <mx:Panel id="panel" visible="true">
            <mx:UIComponent id="uic" />
            <mx:ControlBar>
                  <mx:Button label="Play/Pause" click="ns.togglePause();" />
                  <mx:Button label="Rewind" click="ns.seek(0); ns.pause();" />
                  </mx:ControlBar>
      </mx:Panel>
      
</mx:WindowedApplication>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.