简体   繁体   中英

Flex Spark VideoObject is null

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.

在此处输入图片说明

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] );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM