Playing Sound effects in Android with SoundPool

View more Tutorials:

1- Introduction

This document is based on:
  • Android Studio 4.x

2- SoundPool & AudioManager

First I put out a situation, you are creating a game, it plays out the sounds such as gunfire, bombs, that's the sound effects in the game. Android gives you SoundPool class, it's like a pool  and ready to play sounds when requested.

SoundPool contains a set of source music, the sound source can be from music file in the app or in the file system, .. SoundPool support  play music sources simultaneously.

SourcePool use MediaPlayer service to make a sound.
You can setup SoundPool  to make a sound in specific stream type. There are several types of stream as follows:
Stream Type Description
AudioManager.STREAM_ALARM The audio stream for alarms
AudioManager.STREAM DTMF The audio stream for DTMF Tones
AudioManager.STREAM_MUSIC The audio stream for music playback
AudioManager.STREAM_NOTIFICATION The audio stream for notifications
AudioManager.STREAM_RING The audio stream for the phone ring
AudioManager.STREAM_SYSTEM The audio stream for system sounds
AudioManager.STREAM_VOICE_CALL The audio stream for phone calls
AudioManager allows you to adjust the volume on the different audio streams. For example, you are listening to songs on the device while playing games, you can change the volume of the game without affecting the volume of the song.
Other unmentioned thing is which audio device will produce a sound.

Using STREAM_MUSIC the sound will be produced through one audio device(phone speaker, earphone, bluetooth speaker or something else) connected to the phone.

Using STREAM_RING the sound will be produced through all audio device connected to the phone. This behaviour might be differed for each devices.

SoundPool API:

Phương thức Mô tả
int play(int soundID, float leftVolume, float rightVolume,
            int priority, int loop, float rate)
Play a sound source, and returns the ID of the newly stream is playing (streamID).
void pause(int streamID) Pause sound stream with ID streamID.
void stop(int streamID) Stop sound stream with ID streamID.
void setVolume(int streamID, float leftVolume, float rightVolume) Set volumn for sound stream with ID is streamID
void setLoop(int streamID, int loop) Set loop number for sound stream with ID is streamID.

3- SoundPool example

Create a project named SoundPoolDemo.
  • Name: SoundPoolDemo
  • Package name: org.o7planning.soundpooldemo
Create raw folder is sub-folder of res. Copy 2 music files to raw folder. Here, I copy two sound files: destroy.wav and gun.wav. Note that you can use mp3 or wav audio file extension.
Application interface:
<?xml version="1.0" encoding="utf-8"?>

        app:layout_constraintTop_toTopOf="parent" />

        app:layout_constraintTop_toTopOf="parent" />
package org.o7planning.soundpooldemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private SoundPool soundPool;

    private AudioManager audioManager;
    private Button buttonDestroy;
    private Button buttonGun;

    // Maximumn sound stream.
    private static final int MAX_STREAMS = 5;

    // Stream type.
    private static final int streamType = AudioManager.STREAM_MUSIC;

    private boolean loaded;

    private int soundIdDestroy;
    private int soundIdGun;
    private float volume;

    protected void onCreate(Bundle savedInstanceState) {

        this.buttonDestroy = (Button) this.findViewById(R.id.button_destroy);
        this.buttonGun = (Button) this.findViewById(R.id.button_gun);

        this.buttonDestroy.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

        this.buttonGun.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

        // AudioManager audio settings for adjusting the volume
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        // Current volumn Index of particular stream type.
        float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);

        // Get the maximum volume index for a particular stream type.
        float maxVolumeIndex  = (float) audioManager.getStreamMaxVolume(streamType);

        // Volumn (0 --> 1)
        this.volume = currentVolumeIndex / maxVolumeIndex;

        // Suggests an audio stream whose volume should be changed by
        // the hardware volume controls.

        // For Android SDK >= 21
        if (Build.VERSION.SDK_INT >= 21 ) {
            AudioAttributes audioAttrib = new AudioAttributes.Builder()

            SoundPool.Builder builder= new SoundPool.Builder();

            this.soundPool = builder.build();
        // for Android SDK < 21
        else {
            // SoundPool(int maxStreams, int streamType, int srcQuality)
            this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);

        // When Sound Pool load complete.
        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded = true;

        // Load sound file (destroy.wav) into SoundPool.
        this.soundIdDestroy = this.soundPool.load(this, R.raw.destroy,1);

        // Load sound file (gun.wav) into SoundPool.
        this.soundIdGun = this.soundPool.load(this, R.raw.gun,1);


    // When users click on the button "Gun"
    public void playSoundGun( )  {
        if(loaded)  {
            float leftVolumn = volume;
            float rightVolumn = volume;
            // Play sound of gunfire. Returns the ID of the new stream.
            int streamId = this.soundPool.play(this.soundIdGun,leftVolumn, rightVolumn, 1, 0, 1f);

    // When users click on the button "Destroy"
    public void playSoundDestroy( )  {
        if(loaded)  {
            float leftVolumn = volume;
            float rightVolumn = volume;

            // Play sound objects destroyed. Returns the ID of the new stream.
            int streamId = this.soundPool.play(this.soundIdDestroy,leftVolumn, rightVolumn, 1, 0, 1f);


Running apps:

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.