Not yet rated
Tags:



Problem

Unlike the previous MX version, the spark VideoDisplay component has no attachCamera function. The only other examples of Camera usage I have found have all revolved around attaching to a Video object. The problem is that this means you have to handle all the sizing etc... manually.

Solution

You can create a simple custom MediaElement class and add it to a MediaContainer in a custom UIComponent which will handle all of the sizing issues. This can be included via mxml in the same way VideoDisplay is.

Detailed explanation

package
{
        import flash.events.Event;
        import flash.media.Camera;
        import mx.core.UIComponent;
        import org.osmf.containers.MediaContainer;
        
        public class CameraDisplay extends UIComponent
        {
                private var mediaContainer:MediaContainer;
                private var element:CameraElement;
                
                public function set camera(camera:Camera):void{
                        mediaContainer = new MediaContainer();
                        element = new CameraElement();
                        mediaContainer.width=width;
                        mediaContainer.height=height;
                        addChild(mediaContainer);
                        mediaContainer.addMediaElement(element);
                        element.camera = camera;  //You have to wait until after adding the media element to the mediaContainer before setting the camera property as that will trigger the mediaContainers layout routines.
                }
                
                override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
                {
                        super.updateDisplayList(unscaledWidth, unscaledHeight);
                        if(mediaContainer){
                                mediaContainer.width=unscaledWidth;
                                mediaContainer.height=unscaledHeight;
                        }
                }
        }
}

This can be combined with the following MediaElement extension Class:

package
{
        import flash.media.Camera;
        import flash.media.Video;
        import org.osmf.media.MediaElement;
        import org.osmf.traits.DisplayObjectTrait;
        import org.osmf.traits.MediaTraitType;
        
        public class CameraElement extends MediaElement
        {
                
                public function set camera(camera:Camera):void{
                        var video:Video = new Video(camera.width * 2, camera.height * 2);
                        video.attachCamera(camera);
                        var displayObjectTrait:DisplayObjectTrait = new DisplayObjectTrait(video, camera.width, camera.height);
                        addTrait(MediaTraitType.DISPLAY_OBJECT,displayObjectTrait);
                }
                
        }
}

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes