In this article, we will learn about how to add SoundPool class in android. A collection of audio samples can be loaded into memory from a resource and can be used. SoundPool class is used to play short repeating sound whereas MediaPlayer class is used to play longer clips like music. It uses a MediaPlayer service to decode the audio. Forex:- A game consists of several levels of play. For each level, there is a set of unique sounds that are used only by that level. For that, we used the SoundPool class todo our work. So learning SoundPool class gives a better understanding to deal with the short audio clips.
Approach:
- Create a new Android Resource Directory and for that
right-click on res folder -> Android Resource Directory,
make sure to select resource type as raw. In this raw folder paste your audio clips. - Add the following code in activity_main.xml file. Here three Buttons are added. Each button if clicked will invoke playSound method.
activity_main.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
androidx.constraintlayout.widget.ConstraintLayout
   Â
android:layout_width
=
"match_parent"
   Â
android:layout_height
=
"match_parent"
   Â
android:gravity
=
"center"
   Â
tools:context
=
".MainActivity"
>
Â
ÂÂ Â Â Â
<
Button
       Â
android:id
=
"@+id/button_sound1"
       Â
android:layout_width
=
"wrap_content"
       Â
android:layout_height
=
"wrap_content"
       Â
android:onClick
=
"playSound"
       Â
android:text
=
"Game Over"
       Â
app:layout_constraintEnd_toEndOf
=
"parent"
       Â
app:layout_constraintStart_toStartOf
=
"parent"
       Â
app:layout_constraintTop_toTopOf
=
"parent"
/>
Â
ÂÂ Â Â Â
<
Button
       Â
android:id
=
"@+id/button_sound2"
       Â
android:layout_width
=
"wrap_content"
       Â
android:layout_height
=
"wrap_content"
       Â
android:onClick
=
"playSound"
       Â
android:text
=
"Player Died"
       Â
app:layout_constraintBottom_toBottomOf
=
"parent"
       Â
app:layout_constraintEnd_toEndOf
=
"parent"
       Â
app:layout_constraintTop_toTopOf
=
"parent"
       Â
app:layout_constraintVertical_bias
=
"0.472"
/>
Â
ÂÂ Â Â Â
<
Button
       Â
android:id
=
"@+id/button_sound3"
       Â
android:layout_width
=
"wrap_content"
       Â
android:layout_height
=
"wrap_content"
       Â
android:onClick
=
"playSound"
       Â
android:text
=
"Level Complete"
       Â
app:layout_constraintBottom_toBottomOf
=
"parent"
       Â
app:layout_constraintStart_toStartOf
=
"parent"
       Â
app:layout_constraintTop_toTopOf
=
"parent"
       Â
app:layout_constraintVertical_bias
=
"0.472"
/>
Â
ÂÂ
Â</
androidx.constraintlayout.widget.ConstraintLayout
>
- Add the following code in MainActivity.java file. Here, an object of SoundPool class is created and three audio clips, game_over, level_complete and player_died are added using load method. playSound method is also added which plays the audio clip associated with the respective button.
MainActivity.java
package
org.neveropen.gfgsoundpool;
Â
Âimport
androidx.appcompat.app.AppCompatActivity;
import
android.media.AudioAttributes;
import
android.media.AudioManager;
import
android.media.SoundPool;
import
android.os.Build;
import
android.os.Bundle;
import
android.view.View;
Â
Âpublic
class
MainActivity
   Â
extends
AppCompatActivity {
   Â
SoundPool soundPool;
   Â
int
game_over,
       Â
level_complete,
       Â
player_died;
Â
ÂÂ Â Â Â
@Override
   Â
protected
void
onCreate(
       Â
Bundle savedInstanceState)
   Â
{
       Â
super
.onCreate(savedInstanceState);
       Â
setContentView(R.layout.activity_main);
Â
ÂÂ Â Â Â Â Â Â Â
if
(Build.VERSION.SDK_INT
           Â
>= Build.VERSION_CODES.LOLLIPOP) {
           Â
AudioAttributes
               Â
audioAttributes
               Â
=
new
AudioAttributes
                     Â
.Builder()
                     Â
.setUsage(
                         Â
AudioAttributes
                             Â
.USAGE_ASSISTANCE_SONIFICATION)
                     Â
.setContentType(
                         Â
AudioAttributes
                             Â
.CONTENT_TYPE_SONIFICATION)
                     Â
.build();
           Â
soundPool
               Â
=
new
SoundPool
                     Â
.Builder()
                     Â
.setMaxStreams(
3
)
                     Â
.setAudioAttributes(
                         Â
audioAttributes)
                     Â
.build();
       Â
}
       Â
else
{
           Â
soundPool
               Â
=
new
SoundPool(
                   Â
3
,
                   Â
AudioManager.STREAM_MUSIC,
                   Â
0
);
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// This load function takes
       Â
// three parameter context,
       Â
// file_name and priority.
       Â
game_over
           Â
= soundPool
                 Â
.load(
                     Â
this
,
                     Â
R.raw.game_over,
                     Â
1
);
       Â
level_complete
           Â
= soundPool.load(
               Â
this
,
               Â
R.raw.level_complete,
               Â
1
);
       Â
player_died
           Â
= soundPool.load(
               Â
this
,
               Â
R.raw.player_died,
               Â
1
);
   Â
}
Â
ÂÂ Â Â Â
public
void
playSound(View v)
   Â
{
       Â
switch
(v.getId()) {
Â
ÂÂ Â Â Â Â Â Â Â
case
R.id.button_sound1:
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// This play function
           Â
// takes five parameter
           Â
// leftVolume, rightVolume,
           Â
// priority, loop and rate.
           Â
soundPool.play(
               Â
game_over,
1
,
1
,
0
,
0
,
1
);
           Â
soundPool.autoPause();
           Â
break
;
Â
ÂÂ Â Â Â Â Â Â Â
case
R.id.button_sound2:
           Â
soundPool.play(
               Â
player_died,
1
,
1
,
0
,
0
,
1
);
           Â
break
;
Â
ÂÂ Â Â Â Â Â Â Â
case
R.id.button_sound3:
           Â
soundPool.play(
               Â
level_complete,
1
,
1
,
0
,
0
,
1
);
           Â
break
;
       Â
}
   Â
}
}
Output: