Game Development Reference
In-Depth Information
Listing 6-1. Sound Constructor
Sound(Context iContext, SoundPool Pool, int ResourceID)
{
m_SoundPool = Pool;
m_SoundIndex = m_SoundPool.load(iContext, ResourceID, 1);
}
In terms of playing back a sound, the left speaker volume output
m_LeftVolume
accepts the range
0 to 1.
float m_LeftVolume = 1;
The right speaker volume level
m_RightVolume
also accepts the range 0 to 1.
float m_RightVolume = 1;
In terms of the priority level for playback
m_Priority
(required if resources are limited), the higher the
number, the greater the priority.
int m_Priority = 1;
The variable
m_Loop
determines if the sound is looped or not. A negative value means the sound will
be looped forever. A positive number specifies the number of times to loop the sound. A 0 indicates
that there is no looping.
int m_Loop = 0;
The variable
m_Rate
determines the rate at which to play back the sound. A 1.0 would play the
sound back normally. A 2.0 would play back the sound at twice the rate as normal. The range
is 0.5 to 2.0.
float m_Rate = 1;
The
PlaySound()
function plays back the sound by calling the associated sound pool's
play()
function with the index of the sound
m_SoundIndex
, along with parameters describing how you
should play the sound. By default, we play back the sound at full volume on the left and right
speakers at the normal rate, without any looping of the sound. (See Listing 6-2.)
Listing 6-2. Playing Back a Sound
void PlaySound()
{
/*
* soundID a soundID returned by the load() function
leftVolume left volume value (range = 0.0 to 1.0)
rightVolume right volume value (range = 0.0 to 1.0)
priority stream priority (0 = lowest priority)