Flex中通过duration和useDuration属性设置声音效果长短的例子
By Minidxer | August 27, 2008
接下来的例子演示了Flex中如何通过duration和useDuration属性,设置声音效果长短。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <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>
下面是同样功能的ActionScript实现:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <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>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
No Comments » |
Tags: duration, SoundEffect, useDuration.