0
votes

I am wondering if anyone can help resolve my problem. I am using the NetConnection and NetStream classes to connect to a webcam feed using Flash Media Server. However, this appears every time in my output:

ArgumentError: Error #2126: NetConnection object must be connected. at flash.net::NetStream/ctor() at flash.net::NetStream()

I have tweaked around with the code but to no avail.

Any ideas as to why this isn't working? Here is the (I think relevant) code:

 import flash.net.NetConnection;
 import flash.events.NetStatusEvent;
 import flash.net.NetStream;
 import flash.events.AsyncErrorEvent;

 var nc:NetConnection = new NetConnection();

 nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

 nc.connect("rtmfp://localhost//myUrlExample");

var ns:NetStream = new NetStream(nc);

 ns.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
 ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

 ns.publish("myStream", "recording");

function netHandler(event:NetStatusEvent):void{
    switch(event.info.code){
        case "NetConnection.Connect.Success":
         trace("Successs");
        break;

        case "NetConnection.Connect.Failed":
         trace("Cannot connect to the server");
         break;

         case "NetConnection.Connect.Rejected":
         trace("Ouch!");
        break;
    }
}

function asyncErrorHandler(event:AsyncErrorEvent):void{
        //ignore error;
}
2

2 Answers

0
votes

It seems that you are creating and initializing both NetStream and NetConection before conductor call. Did you try putting that code inside constructor or any other function?

0
votes

The problem is that you need to wait for the NetConnection to actually connect to the server before you try to publish() the NetStream.

You know when the connection has been established from the NetStatusEvent that you're already listening to. So inside the switch statement where the connection attempt succeeds, is when you should wire up the NetStream and publish it:

var nc:NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler); 
nc.connect("rtmfp://localhost//myUrlExample");

var ns:NetStream;

function netHandler(event:NetStatusEvent):void{
    switch(event.info.code){
        case "NetConnection.Connect.Success":
            trace("Successs");
            ns = new NetStream(nc);
            ns.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
            ns.publish("myStream", "recording");
        break;

        case "NetConnection.Connect.Failed":
            trace("Cannot connect to the server");
            break;

        case "NetConnection.Connect.Rejected":
            trace("Ouch!");
            break;
    }
}