2013-07-12 4 views
1

나는 스크린에서 10-30 유닛을 동시에 가지고있는 게임을 쓰려고한다.Android의 여러 객체에 SoundPools를 사용하는 가장 좋은 방법은 무엇입니까?

각 단위는 다른 소리가 있습니다

  • 중립
  • GET은

죽은

  • 공격
  • 을 명중 그래서 완전히 내가 4x30 = 120 wav 파일이 있습니다.

    은 확실히 그것은 어떤 디스패처가 동시에 여러 소리를 재생하는 것을 방지 할 수 있어야합니다.

    내 질문은 :

    내가이 클래스의 모든 장치를 모든 장치 개체에 SoundPool을 추가하거나 SoundPool singelton와 함께 별도의 클래스를 생성하고 관리해야합니까.

    나는 두 가지 옵션을 시도 할 수 있습니다하지만 난 걱정이 메모리 누수 및 성능을 일으킬 수 있습니다.

  • 답변

    2

    당신이 가진 각각의 사운드 파일을 표현하기 위해 아래의 클래스를 사용하여 감사드립니다. 필요에 따라 주위에 보관하고 메모리 누출을 피하기 위해 처리가 끝난 후 처분하십시오.

    public class AndroidSound implements Sound { 
    int soundId; 
    SoundPool soundPool; 
    
    public AndroidSound(SoundPool soundPool, int soundId) { 
        this.soundId = soundId; 
        this.soundPool = soundPool; 
    } 
    
    @Override 
    public void play(float volume) { 
        soundPool.play(soundId, volume, volume, 0, 0, 1); 
    } 
    
    @Override 
    public void dispose() { 
        soundPool.unload(soundId); 
    } 
    

    } 클래스 'newSound 방법 아래

    를 사용하여 당신이 원하는 때마다 재생하고 처분 할 수있는 새로운 사운드 인스턴스를 얻을. 사운드를 만들어 콜렉션에 저장하고 필요할 때 사용할 수 있습니다.

    public class AndroidAudio implements Audio { 
    AssetManager assets; 
    SoundPool soundPool; 
    
    public AndroidAudio(Activity activity) { 
        activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
        this.assets = activity.getAssets(); 
        this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0); 
    } 
    
    @Override 
    public Music newMusic(String filename) { 
        try { 
         AssetFileDescriptor assetDescriptor = assets.openFd(filename); 
         return new AndroidMusic(assetDescriptor); 
        } catch (IOException e) { 
         throw new RuntimeException("Couldn't load music '" + filename + "'"); 
        } 
    } 
    
    @Override 
    public Sound newSound(String filename) { 
        try { 
         AssetFileDescriptor assetDescriptor = assets.openFd(filename); 
         int soundId = soundPool.load(assetDescriptor, 0); 
         return new AndroidSound(soundPool, soundId); 
        } catch (IOException e) { 
         throw new RuntimeException("Couldn't load sound '" + filename + "'"); 
        } 
    } 
    
    +0

    감사합니다, 나는 당신의 접근 방식을 시도 할 것이다. 여러 객체에 대해 설정하려고하면됩니다. –

    관련 문제