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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s