Game Development Reference
In-Depth Information
Hands-on Example: Sounds
In this section, we will add the playing of explosive sounds every time our two cubes from our
previous chapters collide. Each cube will have its own explosive sound, which will be played every
time the cubes collide. For this hands-on example, you will have to download the source code from
the Source Code/Download area of apress.com and install it on your development system to a new
work space. Two sound effects in the form of .wav files have been added to the project and are
located in the res/raw directory.
Modifying the MyGLRenderer Class
For this hands-on example, we will need to add some code to the MyGLRenderer class.
The sound pool we will use to store and play back the sound from is located in m_SoundPool .
private SoundPool m_SoundPool;
The sound index of the explosive sound for our first cube is stored in m_SoundIndex1 .
private int m_SoundIndex1;
The sound index of the explosive sound for our second cube is stored in m_SoundIndex2 .
private int m_SoundIndex2;
The m_SFXOn variable determines if the sound effects are set to on or off.
private boolean m_SFXOn = true;
The CreateSoundPool() function creates the sound pool that is used to create and store sounds for
our cube collisions. (See Listing 6-7.)
The SoundPool constructor accepts the following parameters:
MaxStreams: This is the maximum number of simultaneous streams for this
SoundPool object, which is set to 10.
StreamType: The audio stream type and that for games will normally be
STREAM_MUSIC .
SrcQuality: This is the sample-rate converter quality that currently has no effect
and is set to 0 for the default.
Listing 6-7. Creating the Sound Pool
void CreateSoundPool()
{
int maxStreams = 10;
int streamType = AudioManager.STREAM_MUSIC;
int srcQuality = 0;
 
Search WWH ::




Custom Search