Android SoundPool – How to check if sound file is loaded?

SoundPool loads the sound asynchronously. Before API level 8 where was no standard way of checking if a sound had been loaded before using it.

As of API 8 Android allows to check if the loading has been completed via OnLoadCompleteListener.

Below you find a small example how to use this. It will start playing the sound once the finger touches the display. For the full description please see Sounds with Android.


import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class PlaySound extends Activity implements OnTouchListener {
	private SoundPool soundPool;
	private int soundID;
	boolean loaded = false;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		View view = findViewById(;
		// Set the hardware buttons to control the music
		// Load the sound
		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			public void onLoadComplete(SoundPool soundPool, int sampleId,
					int status) {
				loaded = true;
		soundID = soundPool.load(this, R.raw.sound1, 1);


	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// Getting the user sound settings
			AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
			float actualVolume = (float) audioManager
			float maxVolume = (float) audioManager
			float volume = actualVolume / maxVolume;
			// Is the sound loaded already?
			if (loaded) {, volume, volume, 1, 0, 1f);
				Log.e("Test", "Played sound");
		return false;

I hope this helps.

You find me also on Twitter and Google+.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android and tagged , . Bookmark the permalink.