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
<?xmlversion="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
packageorg.neveropen.gfgsoundpool;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroid.media.AudioAttributes;importandroid.media.AudioManager;importandroid.media.SoundPool;importandroid.os.Build;importandroid.os.Bundle;importandroid.view.View;ÂÂpublicclassMainActivity   ÂextendsAppCompatActivity {   ÂSoundPool soundPool;   Âintgame_over,       Âlevel_complete,       Âplayer_died;   Â@Override   ÂprotectedvoidonCreate(       ÂBundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Âif(Build.VERSION.SDK_INT           Â>= Build.VERSION_CODES.LOLLIPOP) {           ÂAudioAttributes               ÂaudioAttributes               Â=newAudioAttributes                     Â.Builder()                     Â.setUsage(                         ÂAudioAttributes                             Â.USAGE_ASSISTANCE_SONIFICATION)                     Â.setContentType(                         ÂAudioAttributes                             Â.CONTENT_TYPE_SONIFICATION)                     Â.build();           ÂsoundPool               Â=newSoundPool                     Â.Builder()                     Â.setMaxStreams(3)                     Â.setAudioAttributes(                         ÂaudioAttributes)                     Â.build();       Â}       Âelse{           ÂsoundPool               Â=newSoundPool(                   Â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);   Â}   ÂpublicvoidplaySound(View v)   Â{       Âswitch(v.getId()) {       ÂcaseR.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;       ÂcaseR.id.button_sound2:           ÂsoundPool.play(               Âplayer_died,1,1,0,0,1);           Âbreak;       ÂcaseR.id.button_sound3:           ÂsoundPool.play(               Âlevel_complete,1,1,0,0,1);           Âbreak;       Â}   Â}}
Output:
