Jul 19

Flex中如何利用mx.effects SoundEffect类和ProgressBar类的completeEffect样式在进度条(ProgressBar)最后完成时播放一个声音效果的例子中我们了解了如何使用mx.effects SoundEffect类来调用声音效果。接下来的例子演示了Flex中实现调用嵌入声音效果的三种方法。

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


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

①、使用<mx:SoundEffect />标签, @Embed, mouseDownEffect

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
  3.  
  4.     <mx:Script>
  5.         <![CDATA[
  6.             import mx.controls.Alert;
  7.  
  8.             private var alert:Alert;
  9.  
  10.             private function showAlert():void {
  11.                 alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
  12.             }
  13.         ]]>
  14.     </mx:Script>
  15.  
  16.     <mx:SoundEffect id="soundEffect" source="@Embed(source='assets/ding.mp3')" />
  17.  
  18.     <mx:Button label="Delete Internet?" click="showAlert();" mouseDownEffect="{soundEffect}" />
  19.  
  20. </mx:Application>

②、使用 [Embed], <mx:SoundEffect /> ,mouseDownEffect

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
  3.  
  4.     <mx:Script>
  5.         <![CDATA[
  6.             import mx.controls.Alert;
  7.  
  8.             [Bindable]
  9.             [Embed('assets/ding.mp3')]
  10.             private var ding_mp3:Class;
  11.  
  12.             private var alert:Alert;
  13.  
  14.             private function showAlert():void {
  15.                 alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
  16.             }
  17.         ]]>
  18.     </mx:Script>
  19.  
  20.     <mx:SoundEffect id="soundEffect" source="{ding_mp3}" />
  21.  
  22.     <mx:Button label="Delete Internet?" click="showAlert(); " mouseDownEffect="{soundEffect}" />
  23.  
  24. </mx:Application>

③、使用[Embed], SoundAsset类, SoundAsset.play()事件

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">
  3.  
  4.     <mx:Script>
  5.         <![CDATA[
  6.             import mx.controls.Alert;
  7.             import mx.core.SoundAsset;
  8.  
  9.             [Embed('assets/ding.mp3')]
  10.             private var ding_mp3:Class;
  11.  
  12.             private var ding:SoundAsset = new ding_mp3() as SoundAsset;
  13.  
  14.             private var alert:Alert;
  15.  
  16.             private function showAlert():void {
  17.                 alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
  18.             }
  19.         ]]>
  20.     </mx:Script>
  21.  
  22.     <mx:Button label="Delete Internet?" click="showAlert(); ding.play()" />
  23.  
  24. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

written by Minidxer  |  tags: , , ,

Leave a Reply