1 Reply Latest reply on Jun 29, 2006 5:07 PM by mac_martine

    webcam on Flex

    teh_pako
      I've been trying to get a webcam working with Flex and FMS2 with the code below, but everytime i run it i get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at camTest/::connect()
      at camTest/__connectButton_click()

      I'm really not sure what it's telling me to add/remove since it doens't give me any sort of errors before I run it. Here's the code I'm using:

      [CODE]
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

      <mx:Panel title="Cam" borderThickness="1">
      <mx:HBox id="vidBox" width="218" height="120" visible="true"/>
      <mx:ControlBar>
      <mx:Button label="Start" id="connectButton" click="connect();" width="100"/>
      </mx:ControlBar>
      </mx:Panel>


      <mx:Script>
      <![CDATA[
      NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
      SharedObject.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

      private var nc:NetConnection;
      private var webVideo:Video;
      private var inStream:NetStream = null;
      private var camera

      private function init():void{
      nc = new NetConnection();
      nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

      var vidHolder:UIComponent = new UIComponent();
      vidHolder.setActualSize(120, 60);
      webVideo = new Video (120, 60);
      vidHolder.addChild(webVideo);
      webVideo.x = 0;
      webVideo.y = 0;

      vidBox.addChild(vidHolder);
      }


      private function netStatus(event:NetStatusEvent):void {
      var info:Object = event.info;

      switch(info.code){
      case "NetConnection.Connect.Success":
      connectButton.label = "Disconnect";
      connectButton.enabled = true;
      vidBox.visible = true;

      if (inStream) inStream.close();
      //if (outStream) outStream.close();
      inStream = new NetStream(nc);
      //outStream = new NetStream(nc);

      camera = Camera.getCamera();

      if (camera){
      camera.setMode(120, 60, 15, true);
      webVideo.attachCamera(camera);
      inStream.attachCamera(camera);
      }
      inStream.publish("testVideo");

      break;

      case "netConnection.Connect.Closed":
      connectButton.label = "Start";
      connectButton.enabled = true;
      vidBox.visible = true;
      if (inStream) inStream.close();

      break;
      }
      }

      private function connect():void{
      inStream.play("testVideo");
      inStream.client = this;
      webVideo.attachNetStream(inStream);
      switch (connectButton.label){
      case"Start":
      connectButton.enabled = true
      nc.connect("rtmp://198.104.137.82/flex_videoconference");

      break;

      case"Disconnect":
      connectButton.enabled = true;
      nc.close();

      break;
      }
      }
      ]]>
      </mx:Script>
      </mx:Application>

      [/CODE]

      The original file with code can be found here just right click on it and view source.
        • 1. Re: webcam on Flex
          mac_martine Level 1
          In your connect() function, 'instream' is null, so the error is being thrown.

          You never call nc.connect(null), so netStatus() never gets hit which is what makes instream non-null.

          Just add nc.connect(null) after nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

          -Mac