Android provides an extremely useful audio tool in the form of the android.media.SoundPool class. This class allows us to play more than one sound at a time and to adjust the pitch and stereo placement of these sounds programmatically. Here we will create a SoundPool that will play three sound files simultaneously.
You will need three audio files to complete this exercise. Find three short samples in one of the compatible formats, ideally noises that will not sound unpleasant when played together. Here we have used Ogg Vorbis files and named them sound1
, sound2
, and sound3
.
Start up a new Android project in Eclipse and create a new folder called
raw
inside theres
folder.Place the three sound files you selected earlier inside
res/raw
.As a class-wide field in the main Java activity code, declare and assign a new SoundPool as follows:
SoundPool pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
We will have our sounds play...