9

我有一个带有 2 个按钮的 AlertDialog。我希望他们在点击时播放我的自定义声音。所以我在每个按钮上都有这个代码:

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

SoundUtility 是我编写的用于播放自定义声音的类。问题是:它确实播放了我的自定义声音,但同时它也播放了系统音效,所以我同时播放了两个声音。我可以通过重写按钮在常规按钮上禁用它:

public class AppButton extends Button {

public AppButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Disable sound effect
    this.setSoundEffectsEnabled(false);
}

}

然后在我的 XML 文件中:

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

但我找不到在 AlertDialog 按钮上禁用这些系统音效的方法。有什么建议么?

编辑

根据要求,这是 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();
}

}

编辑 2

我的 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
4

2 回答 2

7

试试这个

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

调用setSoundEffectsEnabled您拥有的所有按钮

编辑:

代替

 builder.create().show();

采用

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);
于 2012-12-19T12:44:03.443 回答
2

通过输入主题,我能够在全局范围内关闭声音反馈android:soundEffectsEnabled=false

您可以将主题从清单文件应用到整个应用程序。

替代方式:

您可以创建一个类并在布局文件中使用它...

你的班 :

package com.me.customeapp

public class MeTextView extends TextView {

    public MeTextView (Context context, AttributeSet attrs) {
        this.setSoundEffectsEnabled(false);
    }
}

在您的 xml 文件中放入以下代码:

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

试试看。希望它会帮助你。

于 2012-12-19T12:48:20.447 回答