Mam AlertDialog z 2 przyciskami. Chcę, aby odtwarzali swój niestandardowy dźwięk po kliknięciu. Tak więc mam ten kod na każdym przycisku:Jak wyłączyć efekt dźwiękowy systemu w AlertDialog?
SoundUtility.getInstance(Add_Edit_Note.this).playPositive();
SoundUtility to klasa, którą napisałem, aby odtwarzać niestandardowe dźwięki. Oto problem: odtwarza mój niestandardowy dźwięk, ale jednocześnie odtwarza również dźwiękowy efekt systemu, więc mam dwa dźwięki grające w tym samym czasie. Udało mi się go wyłączyć na standardowych przycisków przez przepisanie przycisk:
public class AppButton extends Button {
public AppButton(Context context, AttributeSet attrs) {
super(context, attrs);
// Disable sound effect
this.setSoundEffectsEnabled(false);
}
}
, a następnie w moim pliku XML:
<com.my.app.AppButton
... />
ale nie mogę znaleźć sposób, aby wyłączyć te efekty dźwiękowe systemu na przyciskach AlertDialog. Jakieś sugestie?
EDIT
Zgodnie z wnioskiem, jest to kod SoundUtility:
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
Kod mojego 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
Myślę, że powinieneś opublikować kod dla swojej klasy SoundUtility, musi to gdzieś odtwarzać efekt dźwiękowy tego systemu. – breadbin
@breadbin Wysłany kod SoundUtility. Ale tak naprawdę nie ma tam nic, co odtwarza ten system dźwiękowy, tylko moje niestandardowe dźwięki ... – Igal