javaandroidaudiolibgdxsoundeffect

Libgdx Sound Effect is not playing Fluently or Completely issue only on Android (lagg effect on sound)


I developed a nice game with libgdx + java, for android as target platform. I am almost at the end, only improving the maps is the remaining task. Till now I was running - debugging my game on desktop because you know libgdx is cross platform and my expectation was just right click and export for android. And of course I tried, then what? all of a sudden I hit the wall. The problem is that my sound effects are not sounding fine on android.They are working great on desktop but not nice on android. Let me analyze the case with a few questions and on examples: if we consider an explosion sound effect: a scenario; vehicle hits to an explosive I catch the contact from box2d and call play method on a explosion sound effect. what I expect is a normal explosion sound. In desktop I hear it, no problem but in android what I hear is just a very very short noisy (really short perhaps a few miliseconds) then a long silence after a while again a short noisy what did I try:

I tried the recommendations from many other threads they are:

  1. I tried it in formats 44.1k hz 16 bit wav file same format ogg file 22.05 k hz wav file they all sound same.
  2. sound effect will not be too short or too long. Okay, it is fine mine is 3 seconds.
  3. I tried to re-save or arrange it with fl-studio and audacity.

I tried them on s5 mini experia z1 and z3 all the same.

mp3 files are working great when I use music class but I have a lot of sound effects and music class is not for that usage so I need to solve this problem

I tried a few other sound files different than my explosion sound effect with same conditions (I mean sound formats frequence or bit rate etc..).

I checked if the sound is loaded completely whether not. yes it is loaded completely but sounds same and of course I am playing it after a while my game starts. With this sentence I mean I waited at least the next frame to play the sound because some guys said that it can not play in the first frame in some thread.

if we want to see some code: how I load the file:

this is called when the game just starts while the user is patiently following the progress bar in loading screen :)

private void loadSoundEffects(){
    assetMan.load("sounds/explosion.wav", Sound.class);
}

then I get the sound into a global sorted map in another method after loading is complete

   soundEffects.put("explosion",assetMan.get("sounds/explosion.wav", Sound.class));

in my sound manager class I have such a method

public void playSingle(String name,float x ,float y){
    Sound sound=null;
    sound=soundEffects.get(name);
    long id=sound.play();
    sound.setVolume(id, getVolume(x, y));//dont worry here volume returns always 1 for texting
}

when I call play method right here it returns a positive id (in case of problem it will return -1)

For loading stuff I also tried this way:

sound=Gdx.audio.newSound(Gdx.files.internal("sounds/explosion.wav"));

this is a big project so I have perhaps more than 70 classes => I can not paste all of them here :) but I feel ready to answer any kind of how,what,where,why,when questions, ask pls.. if you need any more code or file or something just let me know

lets see how it sounds; I uploaded normal explosion wav file and the one I recorded from phone in this rar:

http://www.filedropper.com/soundeffects

you will hear a few sounds like tick tock really it sounds like this :)

EDIT: adding some more info

it is working on windows 7 my OS is win 7

I am using latest libGDX. my IDE is eclipse. I tried it by unplugging the cable to get rid of debugger but result is same.

for sound effect usage file size must be less than 1 mb as wiki says and my file is 688 KB

EDIT: related with performance?

I started to think if it is related with performance of my code. think about some other reason that causes a lag and it makes this effect on sounds but here are some values game works with 55-60 fps even some times all calculations take less than 15 ms but I hear the same sound, same with very little files like 60 KB. So, I will keep thinking about the files. if they are broken? or still a format issue? do you have a working sound effect file? or any suggestions about format? I read somewhere 96khz 16 bit ogg file I tried it as well.

EDIT: important notice

I played my game on mobile. I hit some explosives by car. it was sounding same then I went back to menu. I forgot my phone open. The game was running. But screen is locked automatically. It left like that perhaps 15-20 minutes. Then to try one more time I unlocked the screen and what happened? all of a sudden all explosives exploded I herd the sounds really fine and complete and menu screen was still being displayed. as a result I think I can conclude this when game pauses for enough time sound effects gets ready and can be played well. what does this mean now? sound effects didn't load from memory yet but I am trying to play? or get-ready has another meaning here? I checked if it is loaded or not with asset manager's isLoaded() method it returned true. I did this just before calling the play method on sound. should I trust this isLoaded method? or what?

Well, guys thanks for your helps in advance.

Sorry I kept it a little long I will keep trying and researching..


Solution

  • I found the actual problem and solution for it.

    it is not related with performance or the file or the android. it is a problem in android side of libGDX (may not be considered as a bug but different than desktop version). Let me mark the situation very shortly and then answer:

    I have a lot of sound effect instances. Loaded independently and kept in independent pointers. So there is no relation between those sound effect instances in my code. But they are somehow related in somewhere libGDX. Because case is this: I have a gun class in my game. and the gun has a sound effect. if the fire button pressed gun is continuing to loop its sound effect by calling sound.resume() method and if button is not pressed it is paused with sound.pause() method. in desktop this pause method pauses only the gun's sound effect. but in android it pauses all sound effects or not sure perhaps only the last sound effect that's fired. So in each frame if gun is not firing I call pause method on gun's sound effect (my bad implementation) while calling play for some other sound effects and that pause call on gun cancels all other sound effects. That's how I hear a very short noisy..

    One solution is calling pause method with the id of the sound that will be paused. Another solution is trying for a different implementation that will not need the pause method to be called.

    thanks to anyone who cared my question even he couldn't answer.I think Nahro's answer can stay there because it is also fixing a problem even if that's not my case. If you think that this question won't be helpful to someone in future I can remove it please tell in comment but I think it is a little bit deep, difficult to notice so I guess people will find this post before they find a solution.