How to get sound effect when button clicked in Android or playing with SoundPool

Published by Igor Khrupin on

Here I want to share you a simple code.

If you need to get sound effect for button click or some other event in your Android application you find what you need.

Below you can download source code of this sample Android project

Android SDK provide SoundPool class for it. Lets see how it works.
I’ve created simple application for you. Below you can see screenshot. It is just button which can fire sound effect when it clicked.

Here is sourcecode of Android activity:

package com.hrupin.sample.soundeffects;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundEffectsInAndroidActivity extends Activity implements OnClickListener {
    private Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // init Effects class
        Effects.getInstance().init(this);
        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Effects.getInstance().playSound(Effects.SOUND_1);
    }
}

I’ve created Effects class as wrapper for SoundPool.
Here is sourcecode:

package com.hrupin.sample.soundeffects;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.widget.Toast;

public class Effects {

    private static final String TAG = Effects.class.toString();

    private static final Effects INSTANCE = new Effects();

    // Sound ID can't be 0 (zero)
    public static final int SOUND_1 = 1;

    private Effects() {

    }

    public static Effects getInstance() {
        return INSTANCE;
    }

    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;
    int priority = 1;
    int no_loop = 0;
    private int volume;
    float normal_playback_rate = 1f;

    private Context context;

    public void init(Context context) {
        this.context = context;
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(SOUND_1, soundPool.load(context, R.raw.sound_1, 1));
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public void playSound(int soundId) {
        Log.i(TAG, "!!!!!!!!!!!!!! playSound_1 !!!!!!!!!!");
        soundPool.play(soundId, volume, volume, priority, no_loop, normal_playback_rate);
        Toast.makeText(context, "Now you can hear sound effect!", Toast.LENGTH_LONG).show();
    }
}

Notice, that SOUND_1 constant can’t be 0 (zero).
That’s all for this sample.
Please, write comment if you have some questions. Also you can download the full sourcecode.

Here you can download the source code of simple Android project.

Download it from github

4 Comments

fashion article · 21 August, 2012 at 21:01

After I start your Rss feed it appears to be a ton of junk, is the problem on my side?

Erik · 30 August, 2012 at 21:01

Works!!, thanks!! 😉

Haresh Nathani · 8 December, 2017 at 08:24

Its good…but after given effect i want download effect audio in my devices then what i do…help me.

Ashish Kumar M · 8 August, 2018 at 07:32

Why did you use a hashmap ?.I cant get it !

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.