Creating a SoundPool

Your first job is to build out sound playback inside BeatBox. To do that, first create a SoundPool object (Listing 20.1).

Listing 20.1  Creating a SoundPool (BeatBox.kt)

private const val TAG = "BeatBox"
private const val SOUNDS_FOLDER = "sample_sounds"
private const val MAX_SOUNDS = 5

class BeatBox(private val assets: AssetManager) {

    val sounds: List<Sound>
    private val soundPool = SoundPool.Builder()
        .setMaxStreams(MAX_SOUNDS)
        .build()

    init {
        sounds = loadSounds()
    }
    ...
}

A Builder is used to create a SoundPool instance. The setMaxStreams(Int) option on the builder specifies how many sounds can play at any given time. Here, you pass in 5. If five sounds are playing and you try to play a sixth one, the ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.