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.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity"><Buttonandroid: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"/><Buttonandroid: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"/><Buttonandroid: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;publicclassMainActivityextendsAppCompatActivity {SoundPool soundPool;intgame_over,level_complete,player_died;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {AudioAttributesaudioAttributes=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:

… [Trackback]
[…] There you can find 92886 additional Information to that Topic: geeksforgeeks.org/soundpool-in-android-with-examples/ […]