Maintaining an video’s aspect ratio on a VideoDisplay control in Flex

The following example shows how you can maintain a loaded video’s original aspect ratio on a Flex VideoDisplay control by setting the maintainAspectRatio property.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/30/maintaining-an-videos-aspect-ratio-on-a-videodisplay-control-in-flex/ -->
<mx:Application name="VideoDisplay_maintainAspectRatio_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="maintainAspectRatio:">
                <mx:CheckBox id="checkBox" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:VideoDisplay id="videoDisplay"
            maintainAspectRatio="{checkBox.selected}"
            source="http://helpexamples.com/flash/video/cuepoints.flv"
            autoPlay="false"
            width="200"
            height="200"
            click="videoDisplay.play();" />

</mx:Application>

You can also set the maintainAspectRatio property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/30/maintaining-an-videos-aspect-ratio-on-a-videodisplay-control-in-flex/ -->
<mx:Application name="VideoDisplay_maintainAspectRatio_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function checkBox_change(evt:Event):void {
                videoDisplay.maintainAspectRatio = checkBox.selected;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="maintainAspectRatio:">
                <mx:CheckBox id="checkBox"
                        selected="true"
                        change="checkBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:VideoDisplay id="videoDisplay"
            source="http://helpexamples.com/flash/video/cuepoints.flv"
            autoPlay="false"
            width="200"
            height="200"
            click="videoDisplay.play();" />

</mx:Application>

One thought on “Maintaining an video’s aspect ratio on a VideoDisplay control in Flex

Comments are closed.