2016-07-21 9 views
1

Ich mache ein Spiel mit vielen Sounds und ich benutze Soundpool um sie zu spielen. Bei fast jedem Sound mache ich einen neuen Thread, um im Haupt-UIhread nicht zu viel zu laden. Wie folgt aus:Android - Soundpool spielt nicht alle Sounds mit vielen Apps im Hintergrund ab.

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

Aber wenn ich eine Menge anderer Anwendungen auf dem Handy viele Sounds laufe nicht in meinem Spiel spielen. Wenn ich alle Apps, die auf meinem Handy laufen, killt und dann mein Spiel starte, gibt es keine Probleme mit den Sounds. Scheint wie eine Art von Mangel an Speicher oder etwas. Hat jemand ein ähnliches Problem oder eine Lösung dafür? Irgendwelche Ideen? Vielen Dank!

Ich betreibe mein Spiel auf einem Nexus 5X, Android 6.0

+0

Aus irgendeinem Grund scheint es, dass es Candy Crush Soda ist, die dieses Problem verursacht. Wenn ich Candy Crush Soda abtöte und viele andere Apps im Hintergrund starte, gibt es keine Probleme mit meinen Sounds. – David

Antwort

1

Der Standardthreadpoolgröße ist 5, gehe ich davon aus, dass das, was Sie blockt. Ich denke, du solltest einen SoundManager erstellen.

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; 
    } 
} 

können Sie eine Methode hinzufügen, einen bestimmten Ton zu spielen, wie

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

Ok! Großartig, ich werde das ausprobieren! Ich werde Ihre Antwort akzeptieren, wenn es gut geht! :) Vielen Dank! – David

+0

Humm ... Ich habe es mit nur einem Knopfklick ausprobiert. Es spielt den Sound nur 2 mal, dann gibt es keinen Ton mehr. :(Ich laufe mein Spiel jetzt nur auf dem Handy. Keine Apps im Hintergrund. – David