我希望 能对 Game 中的 游戏音效做一个 统一的管理 ,故设计如下:
利用XML 加载 外部MP3 保存到 MySoundModel 中 ,用 SoundBaseManager.as进行统一的音乐播放管理:
代码:
mySounds.xml:
<?xml version="1.0" encoding="utf-8"?> <root> <sounds> <info id="1" name="CLICK" url="resources/mysound/click.mp3" type="BTN"/> </sounds> </root>
MySoundModel.as:
/** * 音乐MODEL * @author Aonaufly * */ public class MySoundModel{ public var $id : int; public var $name : String; public var $url : String; public var $type : String; }
MySoundVO.as:
/** * 音乐资源类VO * */ public class MySoundVO{ private var $data : Vector.<MySoundModel>; private var $dic_sound : Dictionary; private var $cur_Index : uint = 0; private var $delay : int; private var $try : uint; private var $count : uint; private var $isOK : Boolean = false; //private var $sound : Sound; /** * @param $try : 重试几次加载 * @param $delay : 延迟多少毫秒重新开始加载 * */ public function MySoundVO($try: uint = 2 , $delay : int = 1000){ this.$try = $try; this.$delay = $delay; loaderXML(); } private function loaderXML() : void{ var $loader : URLLoader = new URLLoader(); $loader.addEventListener(Event.COMPLETE,__ $loader.addEventListener(IOErrorEvent.IO_ERROR,__onError); $loader.load(new URLRequest(Global.baseUrl+"resources/mySounds.xml")); } private function __ ( e : Event ) :void{ try{ clearTimeout($count); }catch( e : Error){} var $myxml : XML = new XML(e.target.data); trace("我得到的音乐 : " + $myxml); $data = new Vector.<MySoundModel>(); var $cell : MySoundModel; for each( var $team : XML in $myxml.sounds.info){ $cell = new MySoundModel(); $cell.$id = $team.@id; $cell.$name = $team.@name; $cell.$url = $team.@url; $cell.$type = $team.@type; $data.push($cell); } $count = 0; $dic_sound = new Dictionary(); creatSound(); } /** * 制造一个Sound类 * */ private function creatSound() : void{ if($data != null && $count < $data.length){ var $sound : Sound = new Sound(); $sound.addEventListener(Event.COMPLETE,__onSoundLoaded); $sound.addEventListener(IOErrorEvent.IO_ERROR,__onSoundError); $sound.load(new URLRequest(Global.baseUrl + $data[$count].$url)); //return $sound; }else{ //return null; $isOK = true;//加载完成 } } private function __onSoundLoaded( e : Event ) : void{ var $mySound : Sound = e.target as Sound; var $id : int = $data[$count].$id; $dic_sound[$id] = $mySound; $count += 1; $mySound.removeEventListener(Event.COMPLETE,__onSoundLoaded); $mySound.removeEventListener(IOErrorEvent.IO_ERROR,__onSoundError); creatSound(); } private function __onSoundError( e : IOErrorEvent ) : void{ var $mySound : Sound = e.target as Sound; $mySound.removeEventListener(Event.COMPLETE,__onSoundLoaded); $mySound.removeEventListener(IOErrorEvent.IO_ERROR,__onSoundError); var $id : int = $data[$count].$id; trace("加载 音乐 失败 ID : " + $id); $count += 1; creatSound(); } private function __onError ( e : IOErrorEvent ) : void{ if($cur_Index < $try){ $count = setTimeout(loaderXML,$delay); } $cur_Index += 1; trace("加载音乐出现错误!!!"); } /** * 通过音乐ID得到音乐 * */ public function getSoundByID($soundID : int ) : Sound{ if( null != $dic_sound && $dic_sound[$soundID] != null){ return $dic_sound[$soundID]; }else{ return null; } } /** * 通过音乐Name 得到音乐 * */ public function getSoundByName($soundName : String ) : Sound{ if(null != $data && $data.length > 0){ var $$ii : int = 0; var $$len : uint = $data.length; while($$ii < $$len){ if($data[$$ii].$name == $soundName){ return $dic_sound[($data[$$ii].$id)]; break; } $$ii += 1; } return null; }else{ return null; } } private static var $instance : MySoundVO; public static function get Ins() : MySoundVO{ if( null == $instance ) $instance = new MySoundVO(); return $instance; } private function clone(source:Object):Sound{ var copier:ByteArray=new ByteArray(); copier.writeObject(source); copier.position=0; return(copier.readObject()); } }
SoundBaseManager.as:
/** * 音乐管理类 * @author Aonaufly * */ public class SoundBaseManager{ private static var $instance : SoundBaseManager; public static function get Ins() : SoundBaseManager{ if( null == $instance ) $instance = new SoundBaseManager(); return $instance; } public function SoundBaseManager() : void{ $soundChannelFormat = new SoundTransform(); $soundChannelFormat.volume = 1; } private var $len : uint; private var $channel : SoundChannel; private var $clear : uint = 0; private var $soundChannelFormat : SoundTransform; /*** * 缓存音乐资源 * */ public function catchSounds() : void{ MySoundVO.Ins; } /** * 播放音乐 * @param $id : 音乐的ID号 * @param $len : 播放长度 以毫秒计算 * */ public function playById($id : int,$len : uint = 0) : void{ var $mySound : Sound = MySoundVO.Ins.getSoundByID($id); if(null != $mySound){ this.$len = $len; channelPaly($mySound); } } /** * 播放音乐 * @param $name : 音乐的名称 * @param $len : 播放长度 以毫秒计算 * */ public function playByName($name : String,$len : uint = 0) : void{ var $mySound : Sound = MySoundVO.Ins.getSoundByName($name); if(null != $mySound){ this.$len = $len; channelPaly($mySound); } } private function channelPaly($mySound : Sound):void{ channelStop(); if($channel == null){ $channel = new SoundChannel(); $channel.soundTransform = $soundChannelFormat; } if(this.$len != 0 ){ if(this.$len > $mySound.length){ $len = $mySound.length; } $channel = $mySound.play(0,1); $clear = setTimeout(channelStop,$len); }else{ $channel = $mySound.play(0,1); } } private function channelStop() : void{ if(null != $channel){ $channel.stop(); } if($clear != 0){ try{ clearTimeout($clear); $clear = 0; }catch( e : Error ){} } } }