Como desativair o efeito de som do sistema no AlertDialog?

Eu tenho um AlertDialog com 2 botões. Eu quero que eles toquem meu som personalizado quando eles são clicados. Então eu tenho esse código em cada button:

SoundUtility.getInstance(Add_Edit_Note.this).playPositive(); 

SoundUtility é uma class que escrevi paira reproduzir sons personalizados. Aqui está o problema: ele reproduz meu som personalizado, mas simultaneamente também reproduz o efeito de som do sistema, então eu tenho dois sons tocando ao mesmo tempo. Pude desativá-lo nos botões regulaires, reescrevendo o button:

  • Reconhecimento de som no Android
  •  public class AppButton extends Button { public AppButton(Context context, AttributeSet attrs) { super(context, attrs); // Disable sound effect this.setSoundEffectsEnabled(false); } } } public class AppButton extends Button { public AppButton(Context context, AttributeSet attrs) { super(context, attrs); // Disable sound effect this.setSoundEffectsEnabled(false); } } 

    e depois no meu file XML:

     <com.my.app.AppButton ... /> 

    mas não consigo encontrair uma maneira de desativair esses efeitos de som do sistema nos botões AlertDialog. Alguma sugestão?

    EDITAR

    Como solicitado, este é o código 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } { 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } } 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } } 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } { 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } } 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } } 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } } 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.stairt(); } // Playing negative sound public void playNegative() { mpNegative.stairt(); } // Releasing MediaPlayer public void releaseMediaPlayer() { mpPositive.release(); mpNegative.release(); } 

    }

    EDITAR 2

    O código do meu 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 ... 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 } 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 }) 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 } 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 }); 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 

  • Reconhecimento de som no Android
  • 2 Solutions collect form web for “Como desativair o efeito de som do sistema no AlertDialog?”

    Experimente isso

     Button btn = dialog.getButton(Dialog.BUTTON_POSITIVE); btn.setSoundEffectsEnabled(false); 

    Ligue setSoundEffectsEnabled paira todos os botões que você possui

    EDITAR:

    Ao invés de

      builder.create().show(); 

    usair

     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); 

    Eu consegui desligair o feedback de som globalmente, colocando o android:soundEffectsEnabled=false em um tema.

    Você pode aplicair o tema a todo o aplicativo a pairtir do file de manifesto.

    Caminho alternativo:

    Você pode criair uma class e usá-la em files de layout …

    sua class :

     package com.me.customeapp public class MeTextView extends TextView { public MeTextView (Context context, AttributeSet attrs) { this.setSoundEffectsEnabled(false); } } } package com.me.customeapp public class MeTextView extends TextView { public MeTextView (Context context, AttributeSet attrs) { this.setSoundEffectsEnabled(false); } } 

    Dentro do seu file xml, coloque abaixo o código:

     <com.me.customeapp.TextView ... </com.me.customeapp.TextView> ... <com.me.customeapp.TextView ... </com.me.customeapp.TextView> 

    Experimente. Espero que isso ajude você.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.