2016-07-21 3 views
1

나는 많은 소리로 게임을 만들고 있고, 나는 그들을 재생하기 위해 Soundpool을 사용한다. 거의 모든 사운드에서 메인 UI 스레드에서 많이로드되지 않는 새 스레드를 만듭니다. 이와 같이 :Android - Soundpool은 백그라운드에서 많은 앱으로 모든 소리를 재생하지 않습니다.

public void playSatelliteWinning(final Boolean isSound) { 
    new Thread(new Runnable() { 
     public void run() { 
      if (isSound && !satelliteIsPlaying) { 
       satelliteIsPlaying = true; 
       handler.postDelayed(new Runnable() { 

        public void run() { 
         play(satelliteWinning); 
        } 
       }, 100); 
       satelliteIsPlaying = false; 
      } 
     } 
    }).start(); 
} 

그러나 휴대 전화에서 다른 앱을 많이 쓰고있을 때 많은 소리가 내 게임에서 재생되지 않습니다. 모바일에서 실행되는 모든 앱을 종료하고 게임을 시작하면 소리에 문제가 없습니다. 메모리 부족이나 뭔가 부족한 것 같습니다. 비슷한 문제가 있거나 해결할 사람이 있습니까? 어떤 아이디어? 감사!

나는 6.0

+0

같은 특정 사운드를 재생하는 방법을 추가 할 수 있습니다. 캔디 크러시 소다를 죽이고 백그라운드에서 다른 앱을 많이 사용하면 내 소리에 문제가 없습니다. – David

답변

1

기본 스레드 풀 크기가 5 안드로이드 넥서스 5 배에 내 게임을 실행, 나는 당신을 막고 무엇 가정합니다. SoundManager를 만들어야한다고 생각합니다.

public class SoundManager { 

    public static int SOUNDPOOLSND_MENU_BTN   = 0; 
    public static int SOUNDPOOLSND_WIN    = 1; 
    public static int SOUNDPOOLSND_LOOSE   = 2; 
    public static int SOUNDPOOLSND_DRAW    = 3; 
    public static int SOUNDPOOLSND_TICK1   = 4; 
    public static int SOUNDPOOLSND_TICK2   = 5; 
    public static int SOUNDPOOLSND_OUT_OF_TIME  = 6; 
    public static int SOUNDPOOLSND_HISCORE   = 7; 
    public static int SOUNDPOOLSND_CORRECT_LETTER = 8; 

    public static boolean isSoundTurnedOff; 

    private static SoundManager mSoundManager; 

    private SoundPool mSoundPool; 
    private SparseArray <Integer> mSoundPoolMap; 
    private AudioManager mAudioManager; 

    public static final int maxSounds = 4; 

    public static SoundManager getInstance(Context context) 
    { 
     if (mSoundManager == null){ 
      mSoundManager = new SoundManager(context); 
     } 

     return mSoundManager; 
    } 

    public SoundManager(Context mContext) 
    { 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 
     mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0); 

//  mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
//   public void onLoadComplete(SoundPool soundPool, int sampleId,int status) { 
//    loaded = true; 
//   } 
//  }); 

     mSoundPoolMap = new SparseArray<Integer>(); 
     mSoundPoolMap.put(SOUNDPOOLSND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_WIN, mSoundPool.load(mContext, R.raw.win, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1)); 
     mSoundPoolMap.put(SOUNDPOOLSND_CORRECT_LETTER, mSoundPool.load(mContext, R.raw.correct_letter, 1)); 

     // testing simultaneous playing 
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(0), streamVolume, streamVolume, 1, 20, 1f); 
     mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 2, 1f); 
     mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f); 


    } 

    public void playSound(int index) { 
     if (isSoundTurnedOff) 
      return; 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    } 

    public static void clear() 
    { 
     if (mSoundManager != null){ 
      mSoundManager.mSoundPool = null; 
      mSoundManager.mAudioManager = null; 
      mSoundManager.mSoundPoolMap = null; 
     } 
     mSoundManager = null; 
    } 
} 

당신은이 문제를 일으키는 사탕 호감 소다 것 같습니다 어떤 이유로

public void playSound(SoundsEnum enum){ 
     // play the sound with that value 
    } 
+0

좋습니다! 좋아, 나는 이것을 밖으로 시험 할 것이다! 나는 그것이 잘 작동하면 당신의 대답을 받아 들일 것이다! :) 감사! – David

+0

Humm ... 단 한 번의 버튼 클릭 소리로 시도했습니다. 그것은 단지 2 번만 소리를 내고, 더 이상 소리가 나지 않습니다. (현재 모바일에서만 게임을 실행하고 백그라운드에서는 앱을 사용하지 않습니다. – David

관련 문제