2013-03-15 58 views
1

爲什麼不能連接?當我運行應用程序時,我在輸出中收到NetConnection.failed。你能否仔細看看我的代碼?我相信我的字符串網址是正確的。我不確定我的FMS是否關閉,或者是否有代碼錯誤。無法連接到FMS

謝謝

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); 
+0

' 「RTMFP:/ FMS/streamExample」'不是一個有效的連接字符串。應該看起來像這樣:'「rtmfp://server.com/applicationName」'rtmfp有很多細微差別,首先嚐試讓你的應用程序使用rtmp(注意缺失的'f')。 – BadFeelingAboutThis 2013-03-20 00:26:59

+0

另外,作爲一個補充,你會希望你的網絡連接監聽器在你調用'connect'之前添加爲最佳實踐。 – BadFeelingAboutThis 2013-03-20 00:31:20

回答

1

如果您的Flash Media Server是在同一臺機器上一個比你從測試應用程序,如果你的服務器是另一臺機器上,你應該改變服務器的URL與rtmfp://localhost/streamExample ,那麼您只需將localhost替換爲運行服務器的機器的IP地址。

在執行connect()調用之前,您還應該在您的NetConnection上添加事件偵聽器。

爲了幫助您瞭解失敗的原因,您可能還需要跟蹤事件的描述。

這裏是所有這些跡象表明樣本:

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

謝謝你們!而且,我真的很抱歉遲到的遲到回覆。問題在於字符串地址不正確。出於某種原因,我以爲你把事件監聽器放在nc.connect之後,但是謝謝它的提示! – user2160610 2013-03-25 19:58:53

+0

@ user2160610不要忘記接受答案,如果它幫助你,因爲它可以幫助別人 – duTr 2013-03-25 20:06:45