SoundPool constructor deprecated since API level 21

The good ol’ constructor: SoundPool(int maxStreams, int streamType, int srcQuality) was deprecated in API level 21. We now need to use SoundPool.Builder to create soundPool instance. To use SoundPool in the device running API level >= 21 and API level < 21 just use the following code :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mSoundPool
= new SoundPool.Builder()
.setMaxStreams(10)
.build();
} else {
mSoundPool
= new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
}

Now you can simply use the reference mSoundPool to play sounds as you would usually do.
Keep in mind that I have not used audioAttributes in the example above. If you don’t provide an attribute, the default usage value is USAGE_MEDIA. You can use setAudioAttributes(AudioAttributes) to change them. Learn more about it – SoundPool.Builder() .You can also learn more about the common problems while using soundPool here : SoundPool not Playing sound.

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

You’ve been successfully subscribed to our newsletter! See you on the other side!

Sharing is caring!

Leave a Comment

Your email address will not be published.

Exit mobile version