Does it mean that we cannot use it anymore? What should we use if the min API is set below 21? Also, is it okay to ignore the warning as older applications built using it work on the new OSes?
The old SoundPool
constructor was deprecated in favor of using SoundPool.Builder
to build the SoundPool
object. The old constructor had three parameters: maxStreams
, streamType
, and srcQuality
.
maxStreams
parameter can still be set with the Builder. (And if you don't set it, it defaults to 1.)streamType
parameter is replaced by AudioAttributes
, which is more descriptive than streamType
. (See the different stream type constants starting here.) With AudioAttributes
you can specify the usage (why you are playing the sound), the content type (what you are playing), and flags (how to play it).srcQuality
parameter was supposedly there to set the sample-rate converter quality. However, it was never implemented and setting it had no effect.Thus, SoundPool.Builder
is better than the old constructor because maxStreams
does not need to be explicitly set, AudioAttributes
contains more information than streamType
, and the useless srcQuality
parameter was eliminated. That is why the old constructor was deprecated.
You may still use the old constructor and ignore the warnings if you like. "Deprecated" means that it still works but is no longer the recommended way of doing things.
If you wish to make use of the new constructor while still supporting old versions you can use an if
statement to select the API version.
SoundPool mSoundPool;
int mSoundId;
//...
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);
}
mSoundId = mSoundPool.load(this, R.raw.somesound, 1);
// ...
mSoundPool.play(mSoundId, 1, 1, 1, 0, 1);
Watch this video for more details.
Use SoundPool.Builder
instead. The way a SoundPool is created has been changed. You are encouraged to use the new way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With