Android Gdx Sound Performance Issue
Solution 1:
A little background into the problem about why you notice such a big drop in performance when playing multiple sounds. This is because underneath the covers libgdx needs to mix the sounds together before playing them back to the user. When you get to a certain number of sounds running concurrently the demands on the system to do the mixing becomes noticeably high. Since you are likely trying the play sounds on the rendering thread this increase in demand results in a drop in framerate.
In my experience with any sort of sound effects in games, when you encounter a situation where you have a lot of small sounds playing concurrently there are few solutions. I will write these as more general solutions than specifics for libGDX:
1) Play all sounds on a single thread
This is what you are currently doing, while it doesn't work well once a large number of sounds playing it is easy to implement and for most situations it works. However, once you get enough sounds playing together you will notice lag and poor quality.
2) Play sounds on a separate thread from the rendering thread
This is one possible approach (and depending on if the framework allows), its not very good because it doesn't scale well. However, if you are only suffering minor performance loss and aren't expecting to play a huge number (~20+) of sounds concurrently this is a relatively minor change and will usually fix the problem.
3) Play sounds upto a cap
This solution is a bit more work, and may not be acceptable if you have sounds which must be played. You simply track the number of sounds playing and allow for additional sounds to be played until you hit a certain point. Anything above the cap is simply not played. This allows you to guarantee a certain level of performance. The biggest downside to this method is if you play a sound when your character is critically injured and it doesn't get played, then this may upset the user as they could be listening for the sound to know if they are going to die soon, but instead find themselves dead.
4) Selectively play important sounds
This is a bit more difficult to implement but it works extremely well as you are essentially setting a cap on performance. What you would want to do is determine the priority for a sound to be played. Is it close to you? Is it loud? Is it important? Then, you only play sounds which are important enough to play upto a certain cap (could be 4, 6, 8, etc). You would also need to keep track of how many sounds are currently playing to determine how many new sounds you can play at a given point.
Solution 2:
A work-around: the sounds that are less important to be played to the player, you can play them only if the current FPS is above a certain value. For example:
if (Gdx.graphics.getFramesPerSecond() > 56) playSound();
And important sounds should always be played, no matter the FPS value.
Post a Comment for "Android Gdx Sound Performance Issue"