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!
Asked
Active
Viewed 2,499 times
0
-
2Have you tried [googling it](https://www.google.com/search?q=Android+play+sound)? – Bryan Herbst Oct 02 '13 at 19:02
-
Long/short sound? custom or from Android? – Maxim Shoustin Oct 02 '13 at 19:04
-
@dymmeh, I didn't find that on my first few hits, my bad. – Garima Tiwari Oct 02 '13 at 19:07
2 Answers
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