I would like to learn how to use soundpool method. I would like you to show me a very simple example that run 2 sounds.
Here is a small, working example of soundPool, it is taken from here and slightly modified to match post 21 API's.
One thing to notice is maxStreams, which indicates how many streams are allowed to run in parallel, if it is one(default), it can be removed from the builder.
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager extends Activity
{
static SoundPool soundPool;
static int[] sm;
public static void InitSound() {
int maxStreams = 1;
Context mContext = getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder()
.setMaxStreams(maxStreams)
.build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}
sm = new int[3];
// fill your sounds
sm[0] = soundPool.load(mContext, R.raw.sound_1, 1);
sm[1] = soundPool.load(mContext, R.raw.sound_2, 1);
sm[2] = soundPool.load(mContext, R.raw.sound_3, 1);
}
static void playSound(int sound) {
soundPool.play(sm[sound], 1, 1, 1, 0, 1f);
}
public final void cleanUpIfEnd() {
sm = null;
soundPool.release();
soundPool = null;
}
}