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 :

SoundPool-constructor-deprecated
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.

Sharing is caring!

Leave a Comment

Your email address will not be published.