Skip to main content
October 25, 2013
Answered

Error When Streaming Using AS3

  • October 25, 2013
  • 1 reply
  • 2303 views

Hello Guys, I am trying to stream my webcam using AS3 code but I keep receiving this error message in the output panel when I run the application:

ArgumentError: Error #2126: NetConnection object must be connected.

at flash.net::NetStream/ctor()

at flash.net::NetStream()

At first I listen for the NetStatusEvent status and the connection succeded

But when i include the NetStream class I receive that annoying error. I think this is the relevant code. Any ideas why it isnt working?

import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import flash.media.Camera;
import flash.events.StatusEvent;
import flash.media.Video;
import flash.media.Microphone;

var nc:NetConnection = new NetConnection();

nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

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

var ns:NetStream = new NetStream(nc);

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

function netStatus(event:NetStatusEvent):void{
switch(event.info.code){
 
  case "NetConnection.Connect.Success":
  trace("Connecting up...");
  break;
 
  case "NetConnection.Connect.Failed":
  trace("Unable to Connect");
  break;
 
  case "NetConnection.Connect.Rejected":
  trace("Ouchh!");
  break;
 
  case "NetStream.Connect.Success":
  trace("Thats great!");
  break;
 
  case "NetStream.Connect.Failed":
  trace("Livestream");
  break;
}
}

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

cam.addEventListener(StatusEvent.STATUS, statusHandler);

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

addChild(vid);

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

mic.addEventListener(ActivityEvent.ACTIVITY, this.onMicStatus);


function statusHandler(event:StatusEvent):void{
switch (event.code)
    {
        case "Camera.Muted":
            trace("User clicked Deny.");
            break;
        case "Camera.Unmuted":
            trace("User clicked Accept.");
            break;
    }
}

function onMicStatus(event:StatusEvent):void
{
    if (event.code == "Microphone.Unmuted")
    {
        trace("Microphone access was allowed.");
    } 
    else if (event.code == "Microphone.Muted")
    {
         trace("Microphone access was denied.");
    }
}

This topic has been closed for replies.
Correct answer kglad

your ns.publish() method should not execute until your connection succeeds.  move that line of code.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 25, 2013

your ns.publish() method should not execute until your connection succeeds.  move that line of code.

October 26, 2013

Thanks kglad for your comment. I deleted out the ns.publish method when I executed the code I got a different error...

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at Untitled_fla::MainTimeline/frame1()

Connecting up...

I have been tweaking around with the code but i still couldnt fix the issue. When I debugged the code this appeared in my output:

[SWF] C:\Users\\AppData\Local\Temp\Untitled-1.swf - 3420 bytes after decompression

*** Security Sandbox Violation ***

Connection to rtmfp://localhost/streamCamera halted - not permitted

-- Untrusted local SWFs may not contact the Internet.

SecurityError: Error #2028: Local-with-filesystem SWF file cannot access Internet URL rtmfp://localhost/streamCam.era

at flash.net::NetConnection/connect()

Any advice how to fix this?

kglad
Community Expert
Community Expert
October 26, 2013

you mean you moved that line of code, don't you?