在flex应用程序中链接网络摄像头

时间:2011-09-23 06:37:40

标签: flex actionscript flash-builder

我在连接网络摄像头时出现了一个非常奇怪的问题,我遇到了错误

  

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

以下是我在main.mxml

中的代码
 <fx:Script>
   <![CDATA[
       import flash.media.Camera;
       import flash.media.Video;
       import flash.net.NetConnection;
       import mx.core.UIComponent;
       import com.kahaf.plutay.* ;    

       private var inVideo:Video;
       private var outVideo:Video;
       private var inVideoWrapper:UIComponent;
       private var camera:Camera;
       private var mic:Microphone;
       private var inStream:NetStream;
       private var outStream:NetStream;

       private function defaultVideoMode(): void
       {
          VideoPanel.width = 726;
           VideoPanel.height = 494;
           inVideo.width = 726;
           inVideo.height = 494;
       }

       private function showInComingVideo():void
       {
           inVideo = new Video(VideoPanel.width,VideoPanel.height);
           inVideo.attachNetStream(inStream);
           inVideoWrapper = new UIComponent();
           inVideoWrapper.addChild(inVideo);
           VideoPanel.addElement(inVideoWrapper);
           defaultVideoMode();
        }


       private function setupVideo(event:MouseEvent): void
       {
           camera = Camera.getCamera();
           mic = Microphone.getMicrophone();
           mic.setLoopBack(false); 
           mic.setUseEchoSuppression(true);
           camera.setMode(640,480,20);
           camera.setQuality(65536,90);

           var conn:NetConnection = Connection.getConnection().conn;

           inStream = new NetStream(conn);
           inStream.play(conn);
           showInComingVideo();
       } 
   ]]>

<s:Group x="283" y="330" width="234" height="149" id="VideoPanel" >
</s:Group>
<s:Button x="447" y="151" label="Click Me." click="setupVideo(event)"/>

这是我的连接类的代码:

import flash.net.NetConnection;

public class Connection extends NetConnection
{
    public static var conObj:Connection;
    public var conn:NetConnection;
    public var target:Object;
    public var selector:Function;

    public function Connection()
    {
        conn = new NetConnection;
        target = null;
        selector = null;
        conn.client = this;
}

    public static function getConnection():Connection
    {
        if(conObj == null)
        {
            conObj = new Connection();
        }
            return conObj;
    }
    }

1 个答案:

答案 0 :(得分:0)

处理NetConnection和NetStreams时的正确顺序:

  1. 创建并建立NetConnection(NetConnection.connect())
  2. 等待NetConnection.Connect.Success事件(NetStatusEvent.NET_STATUS)
  3. 创建NetStream并将连接的NetConnection附加到其中
  4. 发布/播放您的信息流