0

I want to be able to add a click sound whenever the uses taps a button in the application, any suggestions as to how I can make the behavior apply to the entire application ? Cheers!

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46

2 Answers2

3

In the onClick of the button, add View.playSoundEffect(SoundEffectConstants.CLICK)

myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            v.playSoundEffect(SoundEffectConstants.CLICK);
        }
    });
hakanostrom
  • 1,081
  • 8
  • 15
0

Or you can use SoundPool for more sophisticated setting (for example you can set the right and left volume values)

This is just an example:

private void playSound() {
        // TODO Auto-generated method stub      
        SoundPool pl = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        // 5 indicates the maximum number of simultaneous streams for this SoundPool object

        int waterSound = pl.load(this, R.raw.water_sound_01, 0);
        // is the audio file I have imported in my project as resource

        pl.setOnLoadCompleteListener(new OnLoadCompleteListener() {             
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                // The onLoadComplet method is called when a sound has completed loading.
                // TODO Auto-generated method stub
                soundPool.play(sampleId, 1f, 1f, 0, 0, 1);
                // second and third parameters indicates left and right value (range = 0.0 to 1.0)
            }
        });
    }
GVillani82
  • 17,196
  • 30
  • 105
  • 172