Playing Sound effects in Android with SoundPool

1- Introduction

This document is based on:
  • Android Studio 1.5

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 to make a sound service.
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.
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:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.o7planning.soundpooldemo.MainActivity">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Destroy"
        android:id="@+id/button_destroy"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="54dp"
        android:onClick="playSoundDestroy" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gun"
        android:id="@+id/button_gun"
        android:layout_alignTop="@+id/button_destroy"
        android:layout_toRightOf="@+id/button_destroy"
        android:layout_toEndOf="@+id/button_destroy"
        android:layout_marginLeft="74dp"
        android:layout_marginStart="74dp"
        android:onClick="playSoundGun" />
    
</RelativeLayout>
MainActivity.java
package org.o7planning.soundpooldemo;

import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {


   private SoundPool soundPool;

   private AudioManager audioManager;

   // 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;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       // 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.
       this.setVolumeControlStream(streamType);

       // For Android SDK >= 21
       if (Build.VERSION.SDK_INT >= 21 ) {

           AudioAttributes audioAttrib = new AudioAttributes.Builder()
                   .setUsage(AudioAttributes.USAGE_GAME)
                   .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                   .build();

           SoundPool.Builder builder= new SoundPool.Builder();
           builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);

           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() {
           @Override
           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(View view)  {
     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(View view)  {
       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: