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>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. Dress Game says:

    I like having the ability to change my video aspect ratio on the fly like in VLC media player.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree