发一个声音管理类 没啥技术含量 就是图个方便~

就不传附件了 免得消耗童鞋们的银子 直接上代码了~(代码没啥技术含量 就不加注释了)

  1. /*声音管理
  2. *@by 古卧猫王 2011.2.18
  3. */
  4. package
  5. {
  6.         import flash.events.Event;
  7.         import flash.events.EventDispatcher;
  8.         import flash.media.Sound;
  9.         import flash.media.SoundChannel;
  10.         import flash.media.SoundMixer;
  11.         import flash.media.SoundTransform;
  12.         import flash.net.URLLoader;
  13.         import flash.net.URLRequest;
  14.         import flash.utils.Dictionary;
  15.        
  16.         public class SoundManager extends EventDispatcher
  17.         {
  18.                 private var soundDic:Dictionary;
  19.                 private var soundChannelDic:Dictionary;
  20.                 private var urlLoader:URLLoader;
  21.                 public function SoundManager()
  22.                 {
  23.                         super();
  24.                         init();
  25.                 }
  26.                 private function init():void{
  27.                         soundDic = new Dictionary();
  28.                         soundChannelDic = new Dictionary();
  29.                         urlLoader = new URLLoader();
  30.                 }
  31.                 public function addSound(id:String,url:String):void{
  32.                         if(soundDic[id]){
  33.                                 return;
  34.                         }
  35.                         var sound:Sound = new Sound();
  36.                         sound.addEventListener(Event.COMPLETE,addComplete);
  37.                         sound.load(new URLRequest(url));
  38.                         function addComplete(e:Event):void{
  39.                                 sound.removeEventListener(Event.COMPLETE,addComplete);
  40.                                 soundDic[id] = sound;
  41.                         }
  42.                 }
  43.                 public function addAndPlaySound(id:String,url:String,loops:int = 0):void{
  44.                         if(soundDic[id]){
  45.                                 return;
  46.                         }
  47.                         var sound:Sound = new Sound();
  48.                         sound.addEventListener(Event.COMPLETE,addAndPlayComplete);
  49.                         sound.load(new URLRequest(url));
  50.                         function addAndPlayComplete(e:Event):void{
  51.                                 sound.removeEventListener(Event.COMPLETE,addAndPlayComplete);
  52.                                 soundDic[id] = sound;
  53.                                 soundChannelDic[id] = sound.play(0,loops);
  54.                         }
  55.                 }
  56.                 public function play(id:String,loops:int = 0):void{
  57.                         if(soundDic[id]){
  58.                                 if(soundChannelDic[id]){
  59.                                         (soundChannelDic[id] as SoundChannel).stop();
  60.                                         soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops);
  61.                                 }else{
  62.                                         soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops);
  63.                                 }
  64.                         }
  65.                 }
  66.                 public function stop(id:String):void{
  67.                         if(soundChannelDic[id]){
  68.                                 (soundChannelDic[id] as SoundChannel).stop();
  69.                         }
  70.                 }
  71.                 public function getSound(id:String):Sound{
  72.                         if(soundDic[id]){
  73.                                 return (soundDic[id] as Sound);
  74.                         }
  75.                         return null;
  76.                 }
  77.                 public function getSoundChannel(id:String):SoundChannel{
  78.                         if(soundChannelDic[id]){
  79.                                 return (soundChannelDic[id] as SoundChannel);
  80.                         }
  81.                         return null;
  82.                 }
  83.                 public function removeSound(id:String):void{
  84.                         if(soundDic[id]){
  85.                                 if(soundChannelDic[id]){
  86.                                         (soundChannelDic[id] as SoundChannel).stop();
  87.                                         soundChannelDic[id] = null;
  88.                                         delete soundChannelDic[id];
  89.                                 }
  90.                                 soundDic[id] = null;
  91.                                 delete soundDic[id];
  92.                         }
  93.                 }
  94.                 public function setVolume(value:Number):void{
  95.                         SoundMixer.soundTransform = new SoundTransform(value);
  96.                 }
  97.                 public function stopAllSound():void{
  98.                         SoundMixer.stopAll();
  99.                 }
  100.         }
  101. }
复制代码

*使用方法:一般是直接在文档类初始化一个静态对象,比如定义为Main.soundManage
*添加一个声音:Main.soundManage.addSound(声音id,声音url地址);//通过id访问管理器中的声音
*添加并播放一个声音:Main.soundManage.addAndPlaySound(声音id,声音url地址,循环次数);//如果需要一直播放背景音,建议把循环次数设置为int.MAX_VALUE
*播放指定声音:Main.soundManage.play(声音id,循环次数);//默认为播放一次
*停止指定声音:Main.soundManage.stop(声音id);
*获得指定声音的Sound对象:Main.soundManage.getSound(声音id);
*获得指定声音的SoundChannel对象:Main.soundManage.getSoundChannel(声音id);
*移除指定声音:Main.soundManage.removeSound(声音id);
*设置音量大小:Main.soundManage.setVolume(声音大小);//0静音~1最大
*停止播放所有声音:Main.soundManage.stopAllSound();

一般游戏这个管理器就够用了 如果不够可以在上面继续改进~

posted @ 2011-08-14 17:33  rob_2010  阅读(318)  评论(0编辑  收藏  举报