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 is responsive: 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 have to load sounds into your SoundPool before you play them. Each sound you load will get its own integer ID. So go ahead and add a mSoundId field to Sound and a generated getter and setter to keep track of this.

Listing 19.2  Adding sound ID field (Sound.java)

p​u​b​l​i​c​ ​c​l​a​s​s​ ​S​o​u​n​d​ ​{​ ​ ​ ​ ​p​r​i​v​a​t​e​ ​S​t​r​i​n​g​ ​m​A​s​s​e​t​P​a​t​h​;​ ​ ​ ​ ​p​r​i​v​a​t​e​ ​S​t​r​i​n​g​ ​m​N​a​m​e​;​ ​ ​ ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.