CHAPTER 12: Digital Audio: Providing Aural Feedback for UI Designs Using SoundPool 463
Next, you will instantiate this SoundPool object inside of the onCreate( ) method, so that when your
application launches, the declared object gets set up (instantiated) for use in your application. Do
this using the Java new keyword, in conjunction with the SoundPool( ) constructor method call,
which accepts three parameters.
Here is the SoundPool( ) constructor method call format from the Android Developer web site
documentation, so you can see it here more clearly before you start to use it in your code:
SoundPool( int maxStreams, int streamType, int srcQuality );
The parameters include a maxStreams value, an AudioManager stream type, and a quality level.
If you type in the AudioManager class and a period, a pop-up dialog will list all of the constants
available to you using this class. Construct the SoundPool object named buttonSamples using the
following code, shown in Figure 12-15:
buttonSamples = new SoundPool(this, AudioManager.STREAM_MUSIC, 100);
Next, create a specialized Array object to hold your SoundPool audio sample references, using an
Android SparseIntArray class and SparseIntArray object. The reason you are doing this is to be
more professional in your coding by using an array structure to hold all of your sample references
(that is, the data path resource locator information for your digital audio assets). Although we are
only using a few digital audio samples during this chapter, often you will be using many more digital
audio samples than this with the SoundPool engine, and therefore I am showing you how to keep all
of these sample references in a SparseIntArray object.
As its name reflects, a SparseIntArray is highly efficient (a sparse use of memory and storage space)
and holds Integer values in an Array programming structure used to hold data.
Declare a SparseIntArray object at the top of your MainActivity class named buttonSampleArray,
and then in the onCreate( ) method, instantiate it using the Java new keyword and a
SparseIntArray( ) constructor method, passing it the array size value of 3, using a standard Java
object constructor method call shown in Figure 12-16:
buttonSampleArray = new SparseIntArray(3);
Figure 12-15. Declare a SoundPool object named buttonSamples and instantiate it in onCreate( ) using the new keyword