Copy link to clipboard
Copied
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.");
}
}
your ns.publish() method should not execute until your connection succeeds. move that line of code.
Copy link to clipboard
Copied
your ns.publish() method should not execute until your connection succeeds. move that line of code.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
you mean you moved that line of code, don't you?
Copy link to clipboard
Copied
Yes, Just clarifying I did remove the ns.publish. Then, I got a new error instead . Its confusing.
Copy link to clipboard
Copied
no, don't remove it. move it to your netconnection listener.
Copy link to clipboard
Copied
Sorry to be a nuisance. I hope I don't sound dumb but you mean move the ns.publish method to the netStatus function like this?
switch (info.code) {case "NetConnection.Connect.Success":
ns.publish("live" "recording");
trace("CONNECTION SUCCES");
break;
Copy link to clipboard
Copied
yes, that should resolve the error message you mentioned.
Copy link to clipboard
Copied
No didnt work! I got the same argument error after putting that line of code in. Sorry, I must be a real pain. Do you put the ns constructor before or after the NetStatus function or doesnt it matter?
Copy link to clipboard
Copied
copy and paste your updated code.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now