Listing 21.1: SoundPool.Builder usage

Istead of just using deprecated constructor, why not to write here something like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_MEDIA)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();
    mSoundPool = new SoundPool.Builder()
        .setAudioAttributes(attributes)
        .setMaxStreams(MAX_SOUNDS)
        .build();
} else {
    //noinspection deprecation
    mSoundPool = new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC, 0);
}

?

1 Like

That’s a better way to do it. I would write similar code in a real project. :+1:

For the purposes of the book, code like this adds an additional hurdle towards understanding how SoundPool works. In general, we try to write less code so that the reader can understand the core concepts.

1 Like