Flex中通过duration和useDuration属性设置声音效果长短的例子

By Minidxer | August 27, 2008

接下来的例子演示了Flex中如何通过duration和useDuration属性,设置声音效果长短。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application name="SoundEffect_useDuration_test"
  3.         xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white">
  7.  
  8.     <mx:SoundEffect id="soundEffect"
  9.             source="http://www.helpexamples.com/flash/sound/song1.mp3"
  10.             useDuration="{checkBox.selected}"
  11.             duration="{slider.value}" />
  12.  
  13.     <mx:ApplicationControlBar dock="true">
  14.         <mx:Form styleName="plain">
  15.             <mx:FormItem label="useDuration:">
  16.                 <mx:CheckBox id="checkBox"
  17.                         selected="true" />
  18.             </mx:FormItem>
  19.             <mx:FormItem label="duration:">
  20.                 <mx:HSlider id="slider"
  21.                         minimum="0"
  22.                         maximum="5000"
  23.                         value="500"
  24.                         snapInterval="100"
  25.                         tickInterval="100"
  26.                         liveDragging="true"
  27.                         showTrackHighlight="true"
  28.                         enabled="{checkBox.selected}" />
  29.             </mx:FormItem>
  30.             <mx:FormItem>
  31.                 <mx:Button id="btn"
  32.                         label="Play sound effect"
  33.                         mouseDown="soundEffect.stop();"
  34.                         mouseDownEffect="soundEffect" />
  35.             </mx:FormItem>
  36.         </mx:Form>
  37.     </mx:ApplicationControlBar>
  38.  
  39. </mx:Application>
下面是同样功能的ActionScript实现:
Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application name="SoundEffect_useDuration_test"
  3.         xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white"
  7.         initialize="init();">
  8.  
  9.     <mx:Script>
  10.         <![CDATA[
  11.             import mx.containers.ApplicationControlBar;
  12.             import mx.containers.Form;
  13.             import mx.containers.FormItem;
  14.             import mx.controls.Button;
  15.             import mx.controls.CheckBox;
  16.             import mx.controls.HSlider;
  17.             import mx.effects.SoundEffect;
  18.             import mx.events.SliderEvent;
  19.  
  20.             private var soundEffect:SoundEffect;
  21.             private var checkBox:CheckBox;
  22.             private var slider:HSlider;
  23.             private var btn:Button;
  24.  
  25.             private function init():void {
  26.                 soundEffect = new SoundEffect();
  27.                 soundEffect.source = "http://www.helpexamples.com/flash/sound/song1.mp3";
  28.  
  29.                 checkBox = new CheckBox();
  30.                 checkBox.selected = true;
  31.                 checkBox.addEventListener(Event.CHANGE, checkBox_change);
  32.  
  33.                 slider = new HSlider();
  34.                 slider.minimum = 0;
  35.                 slider.maximum = 5000; /* 5 seconds */
  36.                 slider.value = soundEffect.duration;
  37.                 slider.snapInterval = 100;
  38.                 slider.tickInterval = 100;
  39.                 slider.liveDragging = true;
  40.                 slider.setStyle("showTrackHighlight", true);
  41.                 slider.addEventListener(SliderEvent.CHANGE, slider_change);
  42.  
  43.                 btn = new Button();
  44.                 btn.label = "Play sound effect"
  45.                 btn.setStyle("mouseDownEffect", soundEffect);
  46.                 btn.addEventListener(MouseEvent.MOUSE_DOWN, btn_mouseDown);
  47.  
  48.                 var formItem1:FormItem = new FormItem();
  49.                 formItem1.label = "useDuration:";
  50.                 formItem1.addChild(checkBox);
  51.  
  52.                 var formItem2:FormItem = new FormItem();
  53.                 formItem2.label = "duration:";
  54.                 formItem2.addChild(slider);
  55.  
  56.                 var formItem3:FormItem = new FormItem();
  57.                 formItem3.addChild(btn);
  58.  
  59.                 var form:Form = new Form();
  60.                 form.styleName = "plain";
  61.                 form.addChild(formItem1);
  62.                 form.addChild(formItem2);
  63.                 form.addChild(formItem3);
  64.  
  65.                 var appControlBar:ApplicationControlBar;
  66.                 appControlBar = new ApplicationControlBar();
  67.                 appControlBar.dock = true;
  68.                 appControlBar.addChild(form);
  69.                 Application.application.addChildAt(appControlBar, 0);
  70.             }
  71.  
  72.             private function checkBox_change(evt:Event):void {
  73.                 soundEffect.useDuration = checkBox.selected;
  74.                 slider.enabled = checkBox.selected;
  75.             }
  76.  
  77.             private function slider_change(evt:SliderEvent):void {
  78.                 soundEffect.duration = evt.value;
  79.             }
  80.  
  81.             private function btn_mouseDown(evt:MouseEvent):void {
  82.                 soundEffect.stop();
  83.             }
  84.         ]]>
  85.     </mx:Script>
  86.  
  87. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Flex | No Comments » | Tags: , ,

Search Posts