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: