Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Why is the constructor for SoundPool deprecated?

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?

like image 303
Harsha Avatar asked Aug 27 '16 17:08

Harsha


2 Answers

Why the SoundPool constructor was deprecated

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.

  • The maxStreams parameter can still be set with the Builder. (And if you don't set it, it defaults to 1.)
  • The 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).
  • The 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.

Using the deprecated constructor to support versions before API 21

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.

like image 153
Suragch Avatar answered Nov 19 '22 18:11

Suragch


Use SoundPool.Builder instead. The way a SoundPool is created has been changed. You are encouraged to use the new way.

like image 3
Augusto Carmo Avatar answered Nov 19 '22 20:11

Augusto Carmo