mydomain
No ADS
No ADS

Playing Sound effects in Android with SoundPool

  1. SoundPool & AudioManager
  2. SoundPool example

1. SoundPool & AudioManager

No ADS
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.

2. SoundPool example

No ADS
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:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_destroy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="50dp"
        android:text="Destroy"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_gun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="50dp"
        android:text="Gun"
        app:layout_constraintStart_toEndOf="@+id/button_destroy"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
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;

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

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

        this.buttonDestroy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSoundDestroy();
            }
        });

        this.buttonGun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSoundGun();
            }
        });

        // 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( )  {
        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:
No ADS

Android Programming Tutorials

Show More
No ADS