0

我想知道是否有人可以帮助解决我的问题。我正在使用 NetConnection 和 NetStream 类连接到使用 Flash Media Server 的网络摄像头源。但是,这每次都会出现在我的输出中:

ArgumentError:错误 #2126:必须连接 NetConnection 对象。在 flash.net::NetStream/ctor() 在 flash.net::NetStream()

我已经调整了代码,但无济于事。

关于为什么这不起作用的任何想法?这是(我认为相关的)代码:

 import flash.net.NetConnection;
 import flash.events.NetStatusEvent;
 import flash.net.NetStream;
 import flash.events.AsyncErrorEvent;

 var nc:NetConnection = new NetConnection();

 nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

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

var ns:NetStream = new NetStream(nc);

 ns.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
 ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

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

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

        case "NetConnection.Connect.Failed":
         trace("Cannot connect to the server");
         break;

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

function asyncErrorHandler(event:AsyncErrorEvent):void{
        //ignore error;
}
4

2 回答 2

0

您似乎在指挥调用之前创建和初始化 NetStream 和 NetConection。您是否尝试将该代码放入构造函数或任何其他函数中?

于 2013-09-22T11:44:49.517 回答
0

问题是您需要等待实际连接到服务器,NetConnection然后再尝试.publish()NetStream

您知道何时建立了NetStatusEvent您已经在收听的连接。因此switch,在连接尝试成功的语句中,您应该连接NetStream并发布它:

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

var ns:NetStream;

function netHandler(event:NetStatusEvent):void{
    switch(event.info.code){
        case "NetConnection.Connect.Success":
            trace("Successs");
            ns = new NetStream(nc);
            ns.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
            ns.publish("myStream", "recording");
        break;

        case "NetConnection.Connect.Failed":
            trace("Cannot connect to the server");
            break;

        case "NetConnection.Connect.Rejected":
            trace("Ouch!");
            break;
    }
}
于 2013-09-25T19:45:48.427 回答