Loading Sounds

The next thing to do with your SoundPool is to load it up with sounds. The main benefit of using a SoundPool over some other methods of playing audio is that SoundPool responds quickly: When you tell it to play a sound, it will play the sound immediately, with no lag.

The trade-off for that is that you must load sounds into your SoundPool before you play them. Each sound you load will get its own integer ID. To track this ID, add a soundId property to Sound.

Listing 20.2  Adding a sound ID property (Sound.kt)

class Sound(val assetPath: String, var soundId: Int? = null) {
    val name = assetPath.split("/").last().removeSuffix(WAV)
}

By making soundId a nullable type (Int?), you make it possible to say that a

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.