Game Development Reference
In-Depth Information
6.2 Adding Sound
Let's begin adding sounds in the main activity:
//Initializes all the media streams, adding them into a soundPool
public void initSound(){
try {
int i=0;
int [] sounds = {
R.raw.click,R.raw.score,R.raw.good,R.raw.pass,R.raw.over
};
//New HashMap to store all the media streams
soundMap = new HashMap<Integer,Integer>();
soundPool
=
new
SoundPool(MAX_SOUND,AudioManager. STREAM_MUSIC ,0);
for ( int sfx : sounds) {
soundMap.put(i++, soundPool.load(RaccoonRob. this , sfx, 1));
}
..........
}
catch (Exception e){}
}
Then implement the playSound(), resumeSound(), stopSound(), and stopAllSound() meth-
ods:
//Plays the stream specified by the streamID
//Parameters:
//
sound - the streamID to be played
//
loop - loop mode, 0 - no loop, -1 - loop forever
public void playSound( int sound, int loop){
try {
soundPool.play(soundMap.get(sound), 1.0f, 1.0f, 1, loop, 1.0f);
Search WWH ::




Custom Search