0

I'm working with spark video components, however the spark videoObject is null, when using a dynamic video source object it still null. Cameras are being detected properly, however when using a variable it's null, when using the Camera object directly usb camera is detected and videoobject still null... any ideas???

Now when using Camera.names all "cameras" are null, when playing a video from apache virtualhosts it plays well, this is so so weird...!

As requested, updated code:

import mx.controls.Alert;
            import mx.events.FlexEvent;
            import spark.components.VideoPlayer;

            private var vidPlyr:VideoPlayer = null;

            protected function winAppCreated(event:FlexEvent):void {
                // Video Player
                vidPlyr = new VideoPlayer();
                vidPlyr.width = 320;
                vidPlyr.height = 240;

                // Video from apache virtualhost:
                vidPlyr.source = "http://flex.test.capimg/JormaKaukonenCracksInTheFinish.flv";
                addElement(vidPlyr);

                var cameraTV:Camera = Camera.getCamera(Camera.names[0]);
                var cameraUSB:Camera = Camera.getCamera(Camera.names[1]);

                if (cameraTV) {
                    vidPlyr.videoDisplay.videoObject.attachCamera(cameraTV);
                } else {
                    Alert.show("no TV card - " + Camera.names[0]);
                    // Alert shows: "no TV card - SAA7130 Analog TV Card" 
                }

                if (cameraUSB) {
                    vidPlyr.videoDisplay.videoObject.attachCamera(cameraUSB);
                } else {
                    Alert.show("no USB camera - " + Camera.names[1]);
                    // Alert shows: "no USB camera - USB2.0 Grabber"
                }
            }

This is a screenshot of running app.

enter image description here

zero323
  • 322,348
  • 103
  • 959
  • 935
CABP
  • 17
  • 6
  • I usually roll my own when it comes to video playback, so I haven't used the Spark `VideoPlayer` class. The [docs](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/components/VideoPlayer.html#videoObject), however, say that if the `source` property is `null` then there may be no underlying `videoObject` yet. Maybe if you show some more code we can help further. – Sunil D. Jul 10 '12 at 16:17
  • Thanks @SunilD. i've added my actual code... – CABP Jul 10 '12 at 16:29

1 Answers1

0

I took a look at the VideoPlayer code, a lot of this class' properties have setters that look like this:

public function set source(value:Object):void
{
    if (videoDisplay)
    {
        // do the real work
    }
    else
    {
        // store the value so we can use it later
     }
}

VideoDisplay is the a skin part of the video player class. When you set the source, the skin must not have initialized the videoObject property. I would set the source, and then wait before trying to attach the camera.

Flex has a callLater() method that may solve this problem. callLater() will execute a function you specify on the next Flex update cycle:

// after setting the source
callLater(attachCamera);


// define a new function 'attachCamera' to call later
private function attachCamera():void
{
    // if the videoObject property is not null
    if (vidPlyr.videoDisplay.videoOjbect != null)
    {
       // attach the camera here
    }
    else
    {
       trace("cannot attach the camera, videoObject is still null");
    }
}

[Edit]

The API to get a camera is strange, the signature is:

public static function getCamera(name:String = null):Camera

But that name argument is not the actual name of the camera. It supposed to be a String representation of the camera's index in the Camera.names array. Quoting the docs:

name:String (default = null) — Specifies which camera to get, as determined from the array returned by the names property. For most applications, get the default camera by omitting this parameter. To specify a value for this parameter, use the string representation of the zero-based index position within the Camera.names array. For example, to specify the third camera in the array, use Camera.getCamera("2").

Try doing something more generic like this when you attach the camera with callLater(attachCamera):

private function attachCamera():void
{
    var cameras:Array = Camera.names;
    var length:int = cameras.length;
    var cameraObjects:Array = [];
    for (var i:int = 0; i < length; i++)
    {
        cameraObjects.push( Camera.getCamera( i.toString() );
    }

    // use your own logic to select a camera, if there's more than one
    if (cameraObjects.length > 0 && vidPlyr.videoDisplay.videoOjbect != null)
    {
        vidPlyr.videoDisplay.videoOjbect.attachCamera( cameraObjects[0] );
    }
}
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • ok, good advice @SunilD thanks for your answer I already attached the camera object... however video is being shown all in black... – CABP Jul 10 '12 at 17:27
  • Correct, you attached the camera object to something that was `null` (the `videoObject` property). That will not do anything except generate a run time exception if you are using the debug Flash player. My suggestion is to wait till after the Spark video player compnonent has a chance to initialize this `videoObject` property -- then attach the camera. – Sunil D. Jul 10 '12 at 18:09
  • yep, i did what you said using callLater function and it works pefectly, video object and camera object are not null anymore, video player works just fine but plays everything "black" any suggestions? =) – CABP Jul 10 '12 at 18:13
  • I should have asked this sooner: does the Spark VideoPlayer class support cameras? To display what my camera is capturing, I use a plain old `Video` object (in Flex you must wrap it in a `UIComponent`), and attach the camera to the `Video` object. Spark VideoPlayer plays recorded/streaming content, but I'm not so sure it is intended for displaying what the camera is capturing (the docs don't say that it does or does not support it). – Sunil D. Jul 10 '12 at 18:19
  • the videoplayer has the videoobject and videodisplay components within, videoobject is the spark equivalent to mx video, videodisplay has another videoobject, both support cameras using the attachcamera method – CABP Jul 10 '12 at 19:09
  • I'm going to add to my answer, this should help. – Sunil D. Jul 10 '12 at 23:52
  • your answer really help, i think my problem is based on the USB device I'm using, it's a EasyCap "USB Grabber" which is NOT a Camera, it's a capturer, everything works fine except playing the video, no errors at all the only messy thing is that video plays everything in black. When using adobe's flash media live encoder i can see video and hear audio... why I it does work with live encode and doesn't work with AS3/MXML ??? – CABP Jul 12 '12 at 22:50