SoundPool doesn’t loop? Android

In my new small Android racing game called ‘Diesel Racer’ I have the need to play an engine sound in a loop.

I play my sounds in Java using SoundPool.

My first attempt to play a sound in a loop was calling the method setLoop  just before playing the sound like so:

Pool.setLoop(sampleId, -1);

That did not have any affect and the sound was playing just once.

I tried to put the same function after I called the play method but it didn’t help either.


I have later found that the play method has a ‘loop’ parameter as well. Since in the past I only needed to play the sound once, I passed 0 to the ‘loop’ parameter which would set the sound to play once.

Setting the ‘loop’ value before calling play will have no effect.

However, I am not sure why setting the ‘loop’ parameter after using play didn’t have any effect either.

In order to play the sound looping forever I just passed -1 to the ‘loop’ parameter when using play.

(I am still not sure how am I suppose to use setLoop).

For the sake of completion here is the code I use to play a SoundPool sound looped:

					try {
						n.SoundID = Pool.load(a.assetManager.openFd(n.Path), Thread.NORM_PRIORITY);
						if (n.IsLoop)
						{
							Pool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
							    public void onLoadComplete(SoundPool Pool, int sampleId,int status) {
									Pool.play(sampleId, 1, 1, 0, -1, 1);
							    }
							});
						}
					} catch (IOException e) {
						e.printStackTrace();
					}

Do notice that this code will loop both ‘.ogg’ and ‘.wav’. I saw some post somewhere that said ‘.ogg’ might not be looped with a SoundPool, but ‘.ogg’ does loop for me with this code.

Warning: Ignoring warnings is bad for you.

Warnings matters.

Programmers might ignore warnings. Both in compilation and in run time.

A warning is not a compilation error, so why are they important?

Think of a warning as increased risk for a horrible crash.

Not only do you want to pay attention to warnings(compilation or run time), you would also want to log warnings yourself. In the case you are developing a framework.

I am working on a new game and my framework works on both the PC and Android.

In the PC version everything worked fine, but in the Android version the collision detection didn’t work.

When I am loading a mesh I am copying its vertex position data so I could use it for collision and physics.

However, on the Android version I was releasing this data in order to save up memory. I also added a method named ‘SetKeep’ to mark a mesh as one that I don’t want to release its physics related data.

I wasn’t working on the Android port of my framework for some time and I completely forgot that I am releasing the collision data on Android but greedily keep it on the PC. That is why it worked on the PC but not on Android.

I just couldn’t understand why my collision data was empty.

A simple log warning mentioning that I am trying to access the collision data without keeping it first would have saved me a lot of time.

Shotgun Practice! Quick game dev.

It took me only a few days to create this small Android game, and to my surprise it is actually a fun game.

I figured I won’t learn how to design games if it takes me 2 years to finish each game. In addition it’s a lot more exciting and motivating to release a game to the Android app store.

So without further ado, please download and try Shotgun Practice(it’s free!):

ShotgunIcon144