Skip to main content
Participant
August 22, 2011
Question

Camera.getCamera() Exception when trying to attachCamera ?

  • August 22, 2011
  • 2 replies
  • 1390 views

Hi,

I try to build a simple Demo of how to use the Camera API with Flex 4.5.1 and AS 3 and Air 2.6 (wether simulating iOS or a real Android HTC Sensation).

The following code:

<?xml version="1.0" encoding="utf-8"?>
     <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark">

<s:Button id="button1" x="347" y="289" label="START" width="122" height="42" click="button1_clickHandler(event)"/>
          <s:TextInput id="textinput1" x="181" y="349" text="App started"/>     
          <s:VideoDisplay id="vd" x="244" y="15" width="546" height="417" />
          <fx:Script>
               <![CDATA[
                    

                    if(Camera.isSupported) {
                         trace("Camera erstellt!");
                         public var myCamera:Camera = new Camera();
                    }
                    
                    
                    protected function button1_clickHandler(event:MouseEvent):void
                    {
                         
                         
                         myCamera = Camera.getCamera();
                         if(myCamera) {
                              textinput1.text ="CAMERA GESETZT!";
                              myCamera.setMode(420,320,20);
                              vd.videoObject.attachCamera(myCamera);
                         }
                    }
               ]]>
          </fx:Script>
          
     </s:Application>




does not work correctly. If I leave out the "vd.videoObject.attachCamera(myCamera)" I do not get any Exception, but also don't see any Videodisplay or Video of my Camera. If I add it, I get the following Error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.


Pointing to the Line of where I use Camera.getCamera()

Main point is that I am not able to see live Video of Camera which I thought should work fine with air for mobile devices?

Planning to do a type of videochat with FMS because of some restrictions to pure Android Code.

This topic has been closed for replies.

2 replies

Participating Frequently
August 23, 2011

I think you are getting an error because the source is not set. If source is null, there is no underlyting Video object yet, and vd.VideoObject will return null. The workaround would be to set the source to any string before you attachCamera. But in that case, the type would be assumed as "any". I would suggest using DynamicStreamingVideoSource object.

-Sanika

iBr3nt
Inspiring
August 22, 2011

Hi,

I'm not sure why it's throwing an error on the vd.videoObject, but the docs mention that the VideoDisplay Spark component is not optimized for mobile. I think the simplest approach is to create a video object and add it to an existing Spark component, like the s:SpriteVisualElement.

Try changing the VideoDisplay to SpriteVisualElement, then within your button handler do something like:

video:Video = new Video();

video.attachCamera(myCamera);

vd.addChild(video);

I successfully got it to work using this code:

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

   xmlns:s="library://ns.adobe.com/flex/spark"

   applicationComplete="init()">

<s:Button id="button1" x="51" y="349" label="START" width="122" height="42" click="button1_clickHandler(event)"/>

<s:TextInput id="textinput1" x="181" y="349" text="App started"/>    

<s:SpriteVisualElement id="vid" x="165" y="10" width="471" height="313"/>

<fx:Script>

<![CDATA[


public var myCamera:Camera;


private function init():void

{

     if(Camera.isSupported) {

          trace("Camera erstellt!");

     }

}

protected function button1_clickHandler(event:MouseEvent):void

{

     myCamera = Camera.getCamera();

     if(myCamera) {

          textinput1.text ="CAMERA GESETZT!";

          myCamera.setMode(420,320,20);

          var video:Video = new Video();

          video.attachCamera(myCamera);

          vid.addChild(video);

     }

}

]]>

</fx:Script>

</s:Application>

iBrent