The following example shows how you can set the duration of a sound effect in Flex by setting the duration and useDuration properties.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/24/setting-the-duration-of-a-sound-effect-in-flex/ -->
<mx:Application name="SoundEffect_useDuration_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:SoundEffect id="soundEffect"
            source="http://www.helpexamples.com/flash/sound/song1.mp3"
            useDuration="{checkBox.selected}"
            duration="{slider.value}" />

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="useDuration:">
                <mx:CheckBox id="checkBox"
                        selected="true" />
            </mx:FormItem>
            <mx:FormItem label="duration:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="5000"
                        value="500"
                        snapInterval="100"
                        tickInterval="100"
                        liveDragging="true"
                        showTrackHighlight="true"
                        enabled="{checkBox.selected}" />
            </mx:FormItem>
            <mx:FormItem>
                <mx:Button id="btn"
                        label="Play sound effect"
                        mouseDown="soundEffect.stop();"
                        mouseDownEffect="soundEffect" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/24/setting-the-duration-of-a-sound-effect-in-flex/ -->
<mx:Application name="SoundEffect_useDuration_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.Button;
            import mx.controls.CheckBox;
            import mx.controls.HSlider;
            import mx.effects.SoundEffect;
            import mx.events.SliderEvent;

            private var soundEffect:SoundEffect;
            private var checkBox:CheckBox;
            private var slider:HSlider;
            private var btn:Button;

            private function init():void {
                soundEffect = new SoundEffect();
                soundEffect.source = "http://www.helpexamples.com/flash/sound/song1.mp3";

                checkBox = new CheckBox();
                checkBox.selected = true;
                checkBox.addEventListener(Event.CHANGE, checkBox_change);

                slider = new HSlider();
                slider.minimum = 0;
                slider.maximum = 5000; /* 5 seconds */
                slider.value = soundEffect.duration;
                slider.snapInterval = 100;
                slider.tickInterval = 100;
                slider.liveDragging = true;
                slider.setStyle("showTrackHighlight", true);
                slider.addEventListener(SliderEvent.CHANGE, slider_change);

                btn = new Button();
                btn.label = "Play sound effect"
                btn.setStyle("mouseDownEffect", soundEffect);
                btn.addEventListener(MouseEvent.MOUSE_DOWN, btn_mouseDown);

                var formItem1:FormItem = new FormItem();
                formItem1.label = "useDuration:";
                formItem1.addChild(checkBox);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "duration:";
                formItem2.addChild(slider);

                var formItem3:FormItem = new FormItem();
                formItem3.addChild(btn);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem1);
                form.addChild(formItem2);
                form.addChild(formItem3);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                Application.application.addChildAt(appControlBar, 0);
            }

            private function checkBox_change(evt:Event):void {
                soundEffect.useDuration = checkBox.selected;
                slider.enabled = checkBox.selected;
            }

            private function slider_change(evt:SliderEvent):void {
                soundEffect.duration = evt.value;
            }

            private function btn_mouseDown(evt:MouseEvent):void {
                soundEffect.stop();
            }
        ]]>
    </mx:Script>

</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.

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