I'm having performance issues when using SoundPool. Every time I play a sound, the frame rate drops. I have added several logs, and I see on logcat that the "play" function sometimes takes 8 ms.
I use * .ogg files, and SoundPool initializes when the application starts:
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mSoundPool = new SoundPool(size, AudioManager.STREAM_MUSIC, 0); mSoundPoolMap = new HashMap<Integer, Integer>(); mSoundPoolMap.put(index, mSoundPool.load(context, R.raw.shot, 1));
To play the sound, I use the following code (inside the game loop):
mSoundPool.play(id, streamVolume, streamVolume, 1, loop, 1f);
My questions:
- Should I call "play" in a different thread, outside the game loop?
- Should I call play through the service?
- Am I doing something wrong?
Thanks!
UPDATE:
I just tested the sound playback in another thread and played the sound through the service, but the lag is still present.
Then I performed the following tests: * play sound at intervals of 1000 ms โ lag always happens * play sound at intervals of 200 ms โ lag NEVER happens
How is this possible?!?!? After these tests, it seems that when nothing happens, SoundPool reboots, and when it plays again, it takes more time to initialize ... very strange!
source share