Android: Why is the constructor for SoundPool deprecated?

12,812

Solution 1

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.

Solution 2

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

Share:
12,812

Related videos on Youtube

Harsha
Author by

Harsha

Happy to help you in any way I can:)

Updated on June 11, 2022

Comments

  • Harsha
    Harsha about 2 years

    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?

    • CommonsWare
      CommonsWare almost 8 years
      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.
    • Harsha
      Harsha almost 8 years
      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?
    • CommonsWare
      CommonsWare almost 8 years
      "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.
    • Harsha
      Harsha almost 8 years
      Thank you, I just used them both in an app to know about it. I also provided an answer about it here: stackoverflow.com/questions/13527134/…
    • Laurel
      Laurel almost 8 years
      Possible duplicate of Deprecated meaning?
  • Harsha
    Harsha almost 8 years
    I looked at it, but I also need to know why it has been deprecated and what happens if it is still used.
  • Augusto Carmo
    Augusto Carmo almost 8 years
    Please, check this question: stackoverflow.com/questions/1999766/…
  • Harsha
    Harsha over 7 years
    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?
  • Suragch
    Suragch over 7 years
    @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, 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).