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.
<?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:
<?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>
