The SoundPool is a very useful class for playing and managing audio resources in Android applications. While using SoundPool class is fairly easy, there are two different constructors that we can use to initialize the SoundPool class, one is for API version less than 21, and one is for API version 21 and greater. You can check both of them out here – Android SoundPool Constructor deprecated.
SoundPool Class Android |
Most common problems as to why the sound player is not playing any sound are :
- SoundPool sample 1 not Ready problem.
- Not passing proper resourse ID to the play method of the SoundPool class.
Before SoundPool can be used, we need to make sure the resources are properly loaded and soundpool is ready to be use. Otherwise when you call the play method, you will get the sampe 1 not Ready message and you won’t hear anything played. To fix this just use onLoadComplete listener and play the sound in side the onLoadComplete callback. Here’s how you use it:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(yourresID, 1, 1, 1, 0, 1);
}
});
In order to play sound from a resource you need to pass the ID that is generated using the load method of soundPool Class, here’s an example :
int mysound = soundPool.load(this, R.raw.mysound, 1);
soundPool.play(mysound, 1, 1, 1, 0, 1);
That’s all, hope your problem is fixed and you can play sounds using SoundPool, check out the new constructor for SoundPool class here – Android SoundPool Constructor deprecated.
If you have any problems, feel free to message me or comment your problem down below! Cheers.
Las vegas recording studios I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.