你的分享就是我们的动力 ---﹥

flex里播放声音的方法

时间:2011-09-14 21:14来源:www.chengxuyuans.com 点击:

flex里播放声音的方法有很多,网上找来找去都只有两种,在这里介绍第三种。

方法三: 这个方法放在前头,是因为网上找不到!(特意强调)

当有很多声音要同时处理时,这种方法非常实用!

在flasf cs3 里的liberty 有声音 可以是 mp3  wav 等,右键选 linkage 填入类名(我这里填BTNsound),然后将它拖到场景中,发布出来(我的命名是sound.swf),放到工程src文件夹里。在flex里新建一个 类(我的Loadsound.as)代码如下:

 package
{
 public class Loadsound
 {
  [Embed(source="Sound.swf",symbol="BTNsound")]//按钮
  public static var btnSound:Class;
 }
}

然后在 mxml 里

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="absolute" creationComplete="init();" >

private var Sbtn:Sound = new Loadsound.btnSound() as Sound;//按钮声音
private var soundVol:SoundTransform = new SoundTransform();//音量

internal function init():void{
      soundVol.volume = 1;
  }

private function onClink():void{

      Sbtn.play(0,0,soundVol);

}

<mx:Button x="129" y="397" label="Button" clink="onClink();"/>

OK!这种方式才是最简单的 最实用的!

方法一:

<mx:SoundEffect id="sound_effect" source="sound.mp3"
        panFrom="-1" panTo="1" loops="1" volumeFrom = "1" volumeTo="0.1" duration="3000" useDuration="false"/>

<mx:Button x="44" y="181" label="Button" width="103" mouseDownEffect="{sound_effect}"/>
<mx:Text x="44" y="145" text="点击鼠标,播放音效" width="129"/>

-------------------------分析------------
1 注意<mx:SoundEffect的source定义了声音的来源,和一般的Effect的target属性不同。
2 注意<mx:Button的mouseDownEffect属性

方法二:

官方文档的 在编译器里 按下 ctrl + f12 搜索sound 就有例子,这里不多说!

package { import flash.display.Sprite; import flash.net.URLRequest; import flash.media.Sound; import flash.media.SoundChannel; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.events.Event; import flash.events.IOErrorEvent; public class Sound_playExample3 extends Sprite { private var snd:Sound = new Sound(); private var channel:SoundChannel; private var statusTextField:TextField = new TextField(); public function Sound_playExample3(){ statusTextField.autoSize = TextFieldAutoSize.LEFT; var req:URLRequest = new URLRequest(http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3); try { snd.load(req); channel = snd.play(); } catch (err:Error) { trace(err.message); } snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); addEventListener(Event.ENTER_FRAME, enterFrameHandler); channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); this.addChild(statusTextField); } private function enterFrameHandler(event:Event):void { var loadTime:Number = snd.bytesLoaded / snd.bytesTotal; var loadPercent:uint = Math.round(100 * loadTime); var estimatedLength:int = Math.ceil(snd.length / (loadTime)); var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength)); statusTextField.text = "Sound file's size is " + snd.bytesTotal + " bytes.\n" + "Bytes being loaded: " + snd.bytesLoaded + "\n" + "Percentage of sound file that is loaded " + loadPercent + "%.\n" + "Sound playback is " + playbackPercent + "% complete."; } private function errorHandler(errorEvent:IOErrorEvent):void { statusTextField.text = "The sound could not be loaded: " + errorEvent.text;        }          e            playing."        removeEventListener(Event.ENTER_FRAME, enterFrameHandler);        }  <!-- -->

本文地址http://www.chengxuyuans.com/Flash/13443.html