24

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?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Harsha
  • 1,568
  • 2
  • 19
  • 22
  • 4
    Questions about "why did Developer X do Thing Y?" are unsuitable for Stack Overflow. Usually, the only party who can provide a clear answer is Developer X, and Developer X is unlikely to see your question. Anyone else can only offer opinions. "Does it mean that we cannot use it anymore?" -- you are welcome to use it, but you should use `SoundPool.Builder` for projects with a `minSdkVersion` of 21 or higher. "What should we use if the min API is set below 21?" -- the `SoundPool` constructor, as you have no choice. – CommonsWare Aug 27 '16 at 18:09
  • Thanks. So does it mean that there would be no issues if I continue using the constructor? Also couldn't I just use MediaPlayer and notice no issues for short sounds? – Harsha Aug 27 '16 at 18:12
  • 1
    "So does it mean that there would be no issues if I continue using the constructor?" -- for the time being, yes. "Also couldn't I just use MediaPlayer and notice no issues for short sounds?" -- `SoundPool` offers features that differ from `MediaPlayer` (e.g., prioritized streams). I would not avoid `SoundPool` merely because its constructor is deprecated. Whether `SoundPool` meets your needs overall, I cannot say. – CommonsWare Aug 27 '16 at 18:27
  • Thank you, I just used them both in an app to know about it. I also provided an answer about it here: http://stackoverflow.com/questions/13527134/audiotrack-soundpool-or-mediaplayer-which-should-i-use/39184503#39184503 – Harsha Aug 27 '16 at 18:39
  • Possible duplicate of [Deprecated meaning?](http://stackoverflow.com/questions/8111774/deprecated-meaning) – Laurel Aug 27 '16 at 22:43

2 Answers2

45

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.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Thanks for the answer, so if this other way of building an object is more descriptive, why not add this as another constructor, as java allows multiple constructers, why did they specifically have to take steps to say that this method is bad to use anymore? – Harsha Mar 26 '17 at 20:45
  • @Harsha, Using a `Builder` is not just adding a new constructor. It is a new class that is used to build (or construct) a `SoundPool`. This design pattern seems to be a trend. (See [`StaticLayout.Builder`](https://developer.android.com/reference/android/text/StaticLayout.Builder.html), for example.) But even if they were just adding a new constructor, it is fairly common to deprecate old constructors that are not recommended any more (see Java [`Date`](http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#constructor_summary)). – Suragch Mar 27 '17 at 02:44
3

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

Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61
  • I looked at it, but I also need to know why it has been deprecated and what happens if it is still used. – Harsha Aug 27 '16 at 18:06
  • Please, check this question: http://stackoverflow.com/questions/1999766/the-constructor-date-is-deprecated-what-does-it-mean-java – Augusto Carmo Aug 27 '16 at 18:08