flex里播放声音的方法

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值