Linking to a webcam using Flash Pro
I was experimenting in my fla file today trying to capture a live webcam
and audio using Flash Professional with ActionScript 3 code. When I hit
CTRL+enter I get this error in my output.
ArgumentError: Error #2126: NetConnection object must be connected at
flash.net::NetStream/ctor
Any ideas as to why this isn't working? Here is the (I think relevant)
code: What ami doing wrong what i am i missing? Interested to hear your
thoughts thanks.
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.media.Camera;
import flash.net.NetStream;
import flash.media.Video;
import flash.media.Microphone;
import flash.events.KeyboardEvent;
import flash.events.Event;
var nc:NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
nc.connect("rtmfp://localhost/cameraExample");
function netHandler(event:NetStatusEvent):void{
switch(event.info.code){
case "NetConnection.Connect.Success":
trace("All connected");
break;
case "NetConnection.Connect.Failed":
trace("Unable to connect sorry");
break;
case "NetConnection.Connect.Rejected":
trace("whoops");
break;
}
}
var ns:NetStream = new NetStream(nc);
ns.attachCamera(camera);
ns.publish("myStream", "live");
var camera:Camera = Camera.getCamera();
camera.setQuality(0,80);
camera.setMode(520,340,15);
camera.addEventListener(StatusEvent.STATUS, statusHandler);
var vid:Video = new Video();
vid.width = camera.width;
vid.height = camera.height;
vid.x = 15;
vid.y = 38;
vid.attachCamera(camera);
addChild(vid);
var mic:Microphone = Microphone.getEnhancedMicrophone();
mic.codec = SoundCodec.SPEEX;
mic.setSilenceLevel(0, 2000);
mic.framesPerPacket = 1;
mic.gain = 50;
ns.attachAudio(mic);
mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
//Verifying that the cameras are installed
if (camera == null)
{
trace("User has no cameras installed.");
}
else
{
trace("User has at least 1 camera installed.");
}
//Detecting permission for the camera access
function statusHandler(event:StatusEvent):void
{
switch (event.code)
{
case "Camera.Muted":
trace("User clicked Deny.");
break;
case "Camera.Unmuted":
trace("User clicked Accept.");
break;
}
}
//camera array names
///////camera lists
for (var i:int = 0; i < Camera.names.length; i++) {
trace(Camera.names[i]);
}
//Microphone access
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.");
}
}
btn_one.addEventListener(MouseEvent.CLICK, doSubmit);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
function doSubmit(event:Event):void{
if(txt_input.text != ""){
txt_area.text += txt_input.text + '\n';
txt_area.verticalScrollPosition = txt_area.maxVerticalScrollPosition;
txt_input.setFocus();
txt_input.text = "";
}
}
function keyHandler(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.ENTER)
if(txt_input.text != ""){
txt_area.text += txt_input.text + '\n';
txt_area.verticalScrollPosition =
txt_area.maxVerticalScrollPosition;
txt_input.setFocus();
txt_input.text = "";
}
}
No comments:
Post a Comment