8 Replies Latest reply on Jan 4, 2010 11:34 AM by Gregory Lafrance

    NetStream.attachCamera

    IlyaG Level 1

      Can some body post an example how can I capture from my cam using NetStream.attachCamera? I am just looking for simple example. The one on "Adobe® Flex™ 3 Language Reference" was a bit hard for me to understand.

        • 1. Re: NetStream.attachCamera
          kkc_mca Level 2

          Here is the example to use the camera and attach to video display
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application name="VideoDisplay_attachCamera_test"
                  xmlns:mx="http://www.adobe.com/2006/mxml"
                  layout="vertical"
                  verticalAlign="middle"
                  backgroundColor="white">
              <mx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
           
                      private function videoDisplay_creationComplete():void {
                          var camera:Camera = Camera.getCamera(); 
                          if (camera) {
                              videoDisplay.attachCamera(camera);
                          } else {
                              Alert.show("You don't seem to have a camera.");
                          }
                      }
                  ]]>
              </mx:Script>
              <mx:VideoDisplay id="videoDisplay"
                      creationComplete="videoDisplay_creationComplete();"
                      width="160"
                      height="120" />
              <mx:Button id="button"
                      label="Reload Camera"
                      click="videoDisplay_creationComplete();" />
          </mx:Application>
          If you find any problem while using this example let me know.
          
          if this post answers your question or helps. Please mark it as such 

          • 2. Re: NetStream.attachCamera
            IlyaG Level 1

            But you didn't used NetStream.attachCamera, you used videoDisplay.attachCamera. It's not what I am looking for.

            • 3. Re: NetStream.attachCamera
              kkc_mca Level 2

              Here is the example to use the camera and attach to video display
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application name="VideoDisplay_attachCamera_test"
                      xmlns:mx="http://www.adobe.com/2006/mxml"
                      layout="vertical"
                      verticalAlign="middle"
                      backgroundColor="white">
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.Alert;
               
                          private function videoDisplay_creationComplete():void {
                              var camera:Camera = Camera.getCamera();
                              var netconnection:NetConnection=new NetConnection();
                              netconnection.connect("rtmp server url other wise null");  
                
                            var netStream:NetStream=new NetStream(netconnection);
                              if (camera) {
                                       netStream.attachCamera(camera);
                              } else {                     Alert.show("You don't seem to have a camera.");                 }             }         ]]>     </mx:Script>     <mx:VideoDisplay id="videoDisplay"             creationComplete="videoDisplay_creationComplete();"             width="160"             height="120" />     <mx:Button id="button"             label="Reload Camera"             click="videoDisplay_creationComplete();" /> </mx:Application>
              If you find any problem while using this example let me know.
              
              if this post answers your question or helps. Please mark it as such 

              • 4. Re: NetStream.attachCamera
                IlyaG Level 1

                It's not working, it tells me "One of the parameters is invalid".

                 

                There is some problem with this line:

                netconnection.connect("rtmp server url other wise null");

                 

                 

                And I didn't understand from your source what is the connection between NetStream and videoDisplay obj?

                 

                But this is exactly what I am looking for, I would really like it if you be able to post a working sample,

                • 6. Re: NetStream.attachCamera
                  Gregory Lafrance Level 6

                  Your URL didn't work for me. This one works:

                   

                  http://livedocs.adobe.com/flex/3/langref/flash/net/NetStream.html#includeExamplesSummary

                   

                  This sample code works:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                    creationComplete="init();">
                    <mx:Script>
                      <![CDATA[
                        import mx.core.UIComponent;
                        private var nc:NetConnectionExample = new NetConnectionExample();
                      
                        private function init():void{
                         var uic:UIComponent = new UIComponent();
                         uic.addChild(nc);
                          addChild(uic);
                        }
                      ]]>
                    </mx:Script>
                  </mx:Application>
                  

                   

                  package {
                      import flash.display.Sprite;
                      import flash.events.NetStatusEvent;
                      import flash.events.SecurityErrorEvent;
                      import flash.media.Video;
                      import flash.net.NetConnection;
                      import flash.net.NetStream;
                      import flash.events.Event;
                      public class NetConnectionExample extends Sprite {
                          private var videoURL:String = "france.flv";
                          private var connection:NetConnection;
                          private var stream:NetStream;
                          public function NetConnectionExample() {
                              connection = new NetConnection();
                              connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
                              connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                              connection.connect(null);
                          }
                          private function netStatusHandler(event:NetStatusEvent):void {
                              switch (event.info.code) {
                                  case "NetConnection.Connect.Success":
                                      connectStream();
                                      break;
                                  case "NetStream.Play.StreamNotFound":
                                      trace("Stream not found: " + videoURL);
                                      break;
                              }
                          }
                          private function securityErrorHandler(event:SecurityErrorEvent):void {
                              trace("securityErrorHandler: " + event);
                          }
                          private function connectStream():void {
                              var stream:NetStream = new NetStream(connection);
                              stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
                              stream.client = new CustomClient();
                              var video:Video = new Video();
                              video.attachNetStream(stream);
                              stream.play(videoURL);
                              addChild(video);
                          }
                      }
                  }
                  class CustomClient {
                      public function onMetaData(info:Object):void {
                          trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
                      }
                      public function onCuePoint(info:Object):void {
                          trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
                      }
                  }
                  

                   

                  If this post answers your question or helps, please mark it as such.


                  Greg Lafrance - Flex 2 and 3 ACE certified

                  www.ChikaraDev.com

                  Flex / AIR Development, Training, and Support Services

                  1 person found this helpful
                  • 7. Re: NetStream.attachCamera
                    IlyaG Level 1

                    But you didn't used the camera, in this example you are running the france.flv file.

                     

                    Can you make an example of using NetStream.attachCamera that will just show my webcam on the screen?

                    • 8. Re: NetStream.attachCamera
                      Gregory Lafrance Level 6

                      Can't get it to wor with Camera either.