How to use SoundPool in a game?

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!

+2
source share
6 answers

I solved the problem by playing a muffled sound in a loop:

 mSoundPool.play(id, 0, 0, 1, -1, 1f); 

The delay has completely disappeared!

This is not like what needs to be done, so if anyone knows how to explain this, let us know!

Additional information: http://www.thiagorosa.com.br/en/how-to/improve-soundpool-performance

+5
source

You can try downloading MP3s instead of OGG, I found that on some phones OGG just doesn't play well.

+1
source

I have a separate drawing and game cycle, and when I call the sound pool, my drawing outline experiences some lag. Not always, but it makes my application seem laggy.

I tried both wav and ogg but it doesn't seem to make much difference. Wav is a little better, but it causes another problem. Soundpool sometimes plays wav files twice when they are called only once, so I switched to ogg.

My current solution is to call the game from a new runnable as:

 public class MyRunnable implements Runnable { private int s_id; public MyRunnable(int sound_id) { s_id = sound_id; } public void run() { SoundManager.playSound(s_id, 1); } } 

and then from my game loop I call

 Runnable r = new MyRunnable(sound_id); handler.postDelayed(r, 0); 

I experience much less delay in this way, but I'm sure this is not the right / best solution.

+1
source

Should I call "play" in a different thread, outside the game loop?

Do I have to call play through the service?

I tried both, and both seemed to offer the same performance boost. Both recorded a visible lag that I noticed in my application. Therefore, I would say yes, select one of them, and it should help you with productivity.

0
source

I just implemented something similar to thiagolr looping technique in the background. I agree that this does not seem to be the correct way to โ€œfixโ€ the problem, but it works. I used MediaPlayer to loop the background โ€œsoundโ€ because if I used the stream from SoundPool, it was sometimes reused to play the actual sound effect and the problem was fixed.

Another thing I think about has helped to convert my entire sound into a 16-bit 22K ogg.

I suppose the biggest drawback of getting stuck in the background is a small battery hit, but it seems worth playing sound effects when they should. I only do this on devices using Gingerbread or below. It works great on the Kindle Fire, and I think it's good on the Nook bar. My application is already a bit on the slow side on Nook Color, but I donโ€™t think this made the situation any worse. The other day I need to do a complete conversion of the drawing code to use OpenGL.

0
source

MP3 is your best bet, as long as OGG is available for all platforms, these platforms may not support it.

0
source

Source: https://habr.com/ru/post/1488040/


All Articles