flex里播放声音的方法有很多,网上找来找去都只有两种,在这里介绍第三种。
方法三:这个方法放在前头,是因为网上找不到!(特意强调)
当有很多声音要同时处理时,这种方法非常实用!
在flasf cs3 里的liberty 有声音 可以是 mp3
{
}
然后在 mxml 里
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
private var Sbtn:Sound = new Loadsound.btnSound() as Sound;//按钮声音
private var soundVol:SoundTransform = new SoundTransform();//音量
internal function init():void{
private function onClink():void{
}
<mx:Button x="129" y="397" label="Button" clink="onClink();"/>
OK!这种方式才是最简单的 最实用的!
方法一:
<mx:SoundEffect id="sound_effect" source="sound.mp3"
<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属性
方法二:
官方文档的
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
errorEvent.text; } e playing." removeEventListener(Event.ENTER_FRAME,
enterFrameHandler); }
<!-- -->
转自:http://edu.gamfe.com/tutor/d/34064.html