July 2015
Beginner to intermediate
600 pages
18h 59m
English
One last step: playing the sounds back. Add the play(Sound) method to BeatBox.
Listing 19.5 Playing sounds back (BeatBox.java)
mSoundPool = new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC, 0);
loadSounds();
}
public void play(Sound sound) {
Integer soundId = sound.getSoundId();
if (soundId == null) {
return;
}
mSoundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
}
public List<Sound> getSounds() {
return mSounds;
}
Before playing your soundId, you check to make sure it is not null.
This might happen if the Sound failed to load.
Once you are sure you have a non-null value, play the sound by calling SoundPool.play(int, float, float, int, int, float). Those parameters are, respectively: the sound ID, volume ...
Read now
Unlock full access