1
votes

Some advice would be welcome. This, is my error in the output...ArgumentError: Error #2126: NetConnection object must be connected at flash.net::NetStream/ctor() at flash.net::NetStream(). Its driving me crazy! I have been tweaking my code but still cannot figure it out. Following is my code I wont show you it all its too long: If you can identify where I am going wrong.

 var nc:NetConnection = new NetConnection();

nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

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

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

        case "NetConnection.Connect.Failed":
        trace("Unable to connect up");
        break;

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

var ns:NetStream = new NetStream(nc);

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

var cam:Camera = Camera.getCamera();
cam.setMode(540, 320, 15);
cam.setQuality(0, 80);
ns.attachCamera(cam);

cam.addEventListener(StatusEvent.STATUS, statusHandler);

var vid:Video = new Video();
vid.width = cam.width;
vid.height = cam.height;
vid.attachCamera(cam);

var mic:Microphone = Microphone.getMicrophone();
mic.setSilenceLevel(0, 2000);
mic.framesPerPacket = 1;
mic.codec = SoundCodec.SPEEX;
mic.gain = 50;
ns.attachAudio(mic);
1

1 Answers

0
votes

It takes some time for the netConnection to establish but the script won't stop while it's connecting, that is why you have the netHandler function.

So you have to set up your netStream after the netStatusEvent "NetConnection.Connect.Success" has triggered.

Your netHandler function should look something like that:

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

        case "NetConnection.Connect.Failed":
        trace("Unable to connect up");
        break;

        case "NetConnection.Connect.Rejected":
        trace("Whoops");
        break;
    }
}
function setupNetStream():void {
    ns = new NetStream(nc);
    // ...
}