Search code examples
javaandroidsoundpool

when using soundPool my first sound loops the rest throw status 12


when using soundPool my first sound loops the rest throw status 12 here is the code

public SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
public HashMap<Integer, Integer> soundsMap;

public int playSound(int sound, int loop) {
   if (curs != sound)
   {
   curs = sound;   
       AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
       float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
       float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
       float volume = streamVolumeCurrent / streamVolumeMax;  
       return soundPool.play(soundsMap.get(sound), volume, volume, 1, loop, 1);

     }else
   {
       return 0;
   }
 }

it only happens when I set loop to -1 they are very small files.


Solution

  • Try to use small size files, with .ogg formate(recommanded), I also added a sleep of 1 sec to make it buffer. outofmemory basically comes if it is not buffered and tried to play

    new Thread() {
                public void run() {
    
                    try {
                        sleep(900);
                        mSoundManager.playSound(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                };
            }.start();