I have a button and I have set android:soundeffectsenabled="true"
.
But this does not trigger the sound effect that I call in my onClick()
method.
@Override
public void onClick(View v) {
int id = v.getId();
switch (id)
{
case(R.id.button):
button.playSoundEffect(SoundEffectConstants.CLICK);
break;
}
}
So was wondering the use of this attribute for a button or any other view.
I noticed that Whatsapp, for example, plays the effect without invoking the MediaPlayer
. I can tell this by how my HTC One M-8 functions where the BoomSound icon is displayed for anything that goes through the MediaPlayer.
Any ideas on this?
Thanks!
After a lot of trial and error, I managed to discover the answer myself. Do note that this was specific to my requirements.
As mentioned I wanted to play a sound like a notification would. This means the volume controls are different for a notification as compared to a MediPlayer
or AudioManager
file being played.
So the way to do this would be:
Uri clickSoundUri = Uri.parse("android.resource://" + **your.package.name** + "/" + **resource of a sound file in the res/raw folder**);
Ringtone r = RingtoneManager.getRingtone(context, clickSoundUri);
r.play();
Thats it. And you can put this code inside your button's onClick()
method and play the sound. This gives the user the option of controlling the audio and notification volumes separately.