1

I am making a game in which the user has to pick up items while passing through a level. I play a sound using a SoundPool object every time the user picks up an item. The sound seems to be causing a slight bit of lag when it is triggered. The thing is, this only occurs on my LG G2, and not my 3 year old asus tablet with significantly less powerful hardware. I'm playing the sounds on the same thread that the game is running in. The following code is how I set up and call my SoundPool objects.

Set up:

sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
gemSound = sp.load(this, R.raw.gemget, 1);

Calling:

sp.play(gemSound, .4f, .4f, 0, 0, 1);

So my question is: Is there a way to make this lag disappear? I'm pretty confused as to what the problem is, since my old, less powerful hardware runs the game butter smooth, and my brand new, powerful hardware is having the issue. Any help is appreciated. Thanks.

EDIT: the sound file is in .mp3 format.

ArmaAK
  • 587
  • 6
  • 21
  • 1
    What format is your sound file in – Ogen Mar 08 '14 at 06:49
  • It is an mp3 file. I've added that to the original question. – ArmaAK Mar 08 '14 at 06:50
  • 1
    You should really use either .wav or .ogg sound files for android. Try using that and see if it's still lagging – Ogen Mar 08 '14 at 06:58
  • Ok thanks, I'll give it a shot. When I was learning, the exercises I did used .mp3 files, so I stuck with it. Out of curiosity, why are .ogg/.wav files better than .mp3 files? – ArmaAK Mar 08 '14 at 07:07
  • The lag is still present with both .ogg and .wav formats. – ArmaAK Mar 08 '14 at 07:29
  • Is the whole code block you posted run each time you pick up an item? – Mike M. Mar 08 '14 at 07:34
  • No, just the last line, sorry. – ArmaAK Mar 08 '14 at 07:35
  • 1
    You might try using a separate Thread. Check it: http://stackoverflow.com/questions/10181822/android-soundpool-play-sometimes-lags – Mike M. Mar 08 '14 at 07:40
  • 1
    AmaAK, have you tried this anser: [http://stackoverflow.com/questions/10181822/android-soundpool-play-sometimes-lags](http://stackoverflow.com/questions/10181822/android-soundpool-play-sometimes-lags) – Void Main Mar 08 '14 at 07:41
  • Yeah I saw that link, but I figured it wasn't a threading issue since my older device was running it fine, but I guess that's my only hope now. Thanks guys. – ArmaAK Mar 08 '14 at 07:44
  • The thread didn't help. I guess I just gotta delve into this and figure it out. – ArmaAK Mar 08 '14 at 08:38
  • So, I figured out two reasons: 1.) I was making some (hundreds of) unnecessary calls to preference objects 2.) I was connected and debugging to my computer via USB, which was the main thing causing the lag. The reason my older tablet was not lagging is because it doesn't send nearly as much data to my PC over USB. Man, the perils of sleep deprived coding. Anyway, thanks for the help and suggestions. – ArmaAK Mar 08 '14 at 09:00

0 Answers0