I have an AlertDialog with 2 buttons. I want them to play my custom sound when they're clicked. So I have this code on every button:
SoundUtility.getInstance(Add_Edit_Note.this).playPositive();
SoundUtility is a class I wrote to play custom sounds. Here's the problem: it does play my custom sound, but simultaneously it also plays the system sound effect, so I have two sounds playing at the same time. I was able to disable it on the regular buttons by rewriting the Button:
public class AppButton extends Button {
public AppButton(Context context, AttributeSet attrs) {
super(context, attrs);
// Disable sound effect
this.setSoundEffectsEnabled(false);
}
}
and then in my XML file:
<com.my.app.AppButton
... />
but I can't find a way to disable these system sound effects on AlertDialog buttons. Any suggestions?
EDIT
As requested, this is the SoundUtility code:
public class SoundUtility {
private static SoundUtility soundInstance;
private MediaPlayer mpPositive;
private MediaPlayer mpNegative;
public static SoundUtility getInstance(Context context){
if(soundInstance==null)
{
soundInstance = new SoundUtility(context);
}
return soundInstance;
}
private SoundUtility (Context context)
{
mpPositive = MediaPlayer.create(context, R.raw.click_positive);
mpNegative = MediaPlayer.create(context, R.raw.click_negative);
}
// Playing positive sound
public void playPositive() {
mpPositive.start();
}
// Playing negative sound
public void playNegative() {
mpNegative.start();
}
// Releasing MediaPlayer
public void releaseMediaPlayer() {
mpPositive.release();
mpNegative.release();
}
}
EDIT 2
The code of my AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to cancel?")
.setCancelable(false) // The dialog is modal, a user must provide an answer
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// If the answer is Yes
public void onClick(DialogInterface dialog, int id) {
...
setResult(RESULT_CANCELED); // Setting result as cancelled and returning it to main activity
SoundUtility.getInstance(Add_Edit_Note.this).playPositive(); // Play positive sound
Add_Edit_Note.this.finish(); // Closing current activity
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
// If the answer is No
public void onClick(DialogInterface dialog, int id) {
SoundUtility.getInstance(Add_Edit_Note.this).playNegative(); // Play negative sound
dialog.cancel(); // Closing the confirmation dialog
}
});
builder.create().show(); // Present the dialog to the user
Try this
Button btn = dialog.getButton(Dialog.BUTTON_POSITIVE);
btn.setSoundEffectsEnabled(false);
Call setSoundEffectsEnabled
for all the buttons that you have
EDIT:
Instead of
builder.create().show();
use
AlertDialog dialog = builder.create();
dialog.show();
Button btn = dialog.getButton(Dialog.BUTTON_POSITIVE);
btn.setSoundEffectsEnabled(false);
Button btn2 = dialog.getButton(Dialog.BUTTON_NEGATIVE);
btn2.setSoundEffectsEnabled(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With