1

Why cant i connect? When i run the application i receive NetConnection.failed in the output. Can you please take a carefull look at my code. I believe my string url is correct. I am unsure is my FMS is down or if theres a code error.

Thank You

import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import flash.media.Camera;
import flash.media.Video;
import flash.media.Microphone;
import fl.controls.Button;

var nc:NetConnection;
var ns:NetStream;
var cam:Camera;
var microphone:Microphone;
var vid:Video;
var connectButton:Button;

//Connecting to Flash Media Server
nc = new NetConnection();

nc.connect("rtmfp:/fms/streamExample");

nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

function netHandler(event:NetStatusEvent):void
{
    switch (event.info.code)
    {
        case "NetConnection.Connect.Success":
        {
            connectButton.label = "Disconnect";
            connectButton.enabled = true;
            sendButton.enabled = true;
            onConnect();
            break;
        }

        case "NetConnection.Connect.Closed":
        {
            connectButton.label = "Connect";
            connectButton.enabled = true;
            sendButton.enabled = false;
            nc.close();
            break;
        }

        case "NetConnection.Connect.Failed":
        {
            trace("Sorry your connection failed");
            break;
        }

        case "NetConnection.Connect.Rejected":
        {
            trace("Oops the connection has been rejected");
            break;
        }
    }
}
ns = new NetStream(nc);

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

ns.attachCamera();


//Setting the video
var vid:Video = new Video();
vid.height = cam.height;
vid.width = cam.width;
vid.x = 10;
vid.y = 10;
addChild(vid);

cam = Camera.getCamera();
cam.setMode(400, 300, 20);
cam.setQuality(0, 85);
cam.setKeyFrameInterval(18);
vid.attachCamera();
addChild(cam);

//Setting the audio
microphone = Microphone.getMicrophone();
microphone.codec = SoundCodec.SPEEX;
microphone.framesPerPacket = 1;
microphone.setSilenceLevel(0, 200);
microphone.gain = 50;
NetStream.attachAudio(microphone);
duTr
  • 714
  • 5
  • 21
  • `"rtmfp:/fms/streamExample"` is not a valid connection string. Should look like this: `"rtmfp://server.com/applicationName"` There are a lot of nuances with rtmfp, try getting your application working with rtmp (note the missing 'f') first. – BadFeelingAboutThis Mar 20 '13 at 00:26
  • Also, as an aside, you'll want your net connection listeners added before you call `connect` as a best practice. – BadFeelingAboutThis Mar 20 '13 at 00:31

1 Answers1

1

If your Flash Media Server is on the same machine than the one your testing your application from, you should change the URL of server with rtmfp://localhost/streamExample If your server is on another machine, then you just have to replace localhost with the IP address of the machine running the server.

You should also add the event listener on your NetConnection before you perform the connect() call.

To help you known the reason of the failure, you might want to trace the description of your event as well.

Here is a sample with all these indications:

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

function netHandler(event:NetStatusEvent):void
{
    switch (event.info.code)
    {
        case "NetConnection.Connect.Success":
        {
            connectButton.label = "Disconnect";
            connectButton.enabled = true;
            sendButton.enabled = true;
            onConnect();
            break;
        }
        case "NetConnection.Connect.Closed":
        {
            connectButton.label = "Connect";
            connectButton.enabled = true;
            sendButton.enabled = false;
            nc.close();
            break;
        }
        case "NetConnection.Connect.Failed":
        {
            trace("Sorry your connection failed");
            trace("Reason is: " + event.info.description);
            break;
        }
        case "NetConnection.Connect.Rejected":
        {
            trace("Oops the connection has been rejected");
            break;
        }
    }
}
duTr
  • 714
  • 5
  • 21
  • Thank you guys! And, im really sorry for my late belated reply. Problem was with the string address it was incorrect. For some reason I thought you put the the event listener after the nc.connect but thanks for the tip anyway it worked! – user2160610 Mar 25 '13 at 19:58
  • @user2160610 Don't forget to accept the answer if it helped you as it could help someone else – duTr Mar 25 '13 at 20:06