2013-09-21 33 views
0

我想知道是否有人可以幫助解決我的問題。我正在使用NetConnection和NetStream類使用Flash Media Server連接到網絡攝像頭饋送。但是,每次在我的輸出中都會出現:使用ActionScript編寫編程錯誤

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; 
} 

回答

0

看起來你是在創建並初始化導體調用之前的NetStream和NetConection。你是否嘗試將該代碼放入構造函數或其他函數中?

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; 
    } 
}