AS3 음향 효과 관리

저 는 Game 의 게임 음향 효과 에 대해 통 일 된 관 리 를 하고 싶 습 니 다. 그래서 디자인 은 다음 과 같 습 니 다.
XML 로 외부 MP3 를 불 러 와 MySoundModel 에 저장 하고 SoundBaseManager. as 로 음악 재생 관 리 를 통일 합 니 다.
코드:
mySounds.xml:


    
        
    

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.;
    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.();
        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 ){}
        }
    }
}

좋은 웹페이지 즐겨찾기