How to properly use SoundPool on a game?

筅森魡賤 提交于 2019-11-27 23:22:42

I solved the issue by playing a muted sound in loop:

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

The lag disappeared completely!

This doesn't seem the right thing to do, so if anyone knows how to explain this, please let us know!

More Info: http://www.thiagorosa.com.br/en/how-to/improve-soundpool-performance

You could try to load MP3s instead of OGGs, I found that on some phones OGGs just don't play well.

I have a seperate drawing and game loop, and when I call the sound pool play, my drawing loop experience some lag. Not always, but it makes my app seems laggy.

I've tried both wav and ogg, but doesnt seem to have much difference. Wav is a bit better, but it causes another problem, soundpool sometimes plays wav files twice when they called only once, so I switched to ogg.

My current solution is to call play from 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 lag in this way, but I'm sure it's not the correct/best solution.

Should I call "play" on another thread, outside the game loop?

Should I call "play" through a service?

I've tried both and they both seemed to offer an identical performance boost. Both did fix the visible lag that I noticed within my application though. So I would say yes pick one of those to do and it should help out your performance.

I just implemented something similar to thiagolr's technique of looping silence in the background. I agree that it doesn't seem like the right way to "fix" the problem--but it works. I used a MediaPlayer to loop the background "sound," because if I used a stream from the SoundPool, it sometimes got reused to play an actual sound effect, and the problem was back.

Another thing that I think helped was converting all of my audio to 16-bit 22K ogg.

I suppose the biggest downside to looping silence in the background is a small battery hit, but it seems worth it to have sound effects play when they're supposed to. I only do this on devices using Gingerbread or below. It works wonderfully well on the Kindle Fire, and I think adequately well on the Nook Tablet. My app is already a little on the slow side on the Nook Color, but I don't think this made it any worse. One of these days I need to do a complete conversion of my drawing code to use OpenGL.

MP3 is pretty much your best bet since while OGG is provided for all platforms, those platforms may not support it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!