android - 如何禁用所有按钮的声音效果?

标签 android audio soundeffect

我有不同的按钮,每个按钮都有声音效果。为了设置声音效果,我使用了此类:

public class Effects {
    private static final String TAG = Effects.class.toString();

    private static final Effects INSTANCE = new Effects();


    public static final int SOUND_1 = 1;
    public static final int SOUND_2 = 2;

    private Effects() {

    }

    public static Effects getInstance() {
        return INSTANCE;
    }

    private SoundPool soundPool;
    private HashMap<Integer, Integer> soundPoolMap;
    int priority = 1;
    int no_loop = 0;
    private int volume;
    float normal_playback_rate = 1f;

    private Context context;

    public void init(Context context) {
        this.context = context;
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(SOUND_1, soundPool.load(context, R.raw.laser, 1));

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public void playSound(int soundId) {
        Log.i(TAG, "!!!!!!!!!!!!!! playSound_1 !!!!!!!!!!");
        soundPool.play(soundId, volume, volume, priority, no_loop, normal_playback_rate);

    }
    ...
}

然后在我的 Activity 中,我使用此代码确定要应用声音的类(class)。
Effects.getInstance().init(this);

在我点击按钮时:
Effects.getInstance().playSound(Effects.SOUND_1);

它可以正常工作。
但是现在我想要另一个按钮如禁用禁用我所有按钮的声音。当我单击按钮(禁用)时,我使用了以下代码:
 button(my_button_name).setSoundEffectsEnabled(false);

但这不起作用。
有什么问题?

最佳答案

向您添加您的Effects类 bool(boolean) 变量mSoundEffectsEnabled和方法

public void setSoundEffectsEnabled(boolean enabled){
    mSoundEffectsEnabled = enabled;
}

并像这样更改您的方法:
public void init(Context context) {
    this.context = context;
    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
    soundPoolMap = new HashMap<Integer, Integer>();
    soundPoolMap.put(SOUND_1, soundPool.load(context, R.raw.laser, 1));

    AudioManager audioManager = (AudioManager)     context.getSystemService(Context.AUDIO_SERVICE);
    volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    setSoundEffectsEnabled(true);
 }

public void playSound(int soundId) {
    if (mSoundEffectsEnabled){
        Log.i(TAG, "!!!!!!!!!!!!!! playSound_1 !!!!!!!!!!");
        soundPool.play(soundId, volume, volume, priority, no_loop,     normal_playback_rate);
    }
}

现在,您可以随时通过调用Effects.getInstance().setSoundEffectsEnabled(flag)禁用或启用声音效果

关于android - 如何禁用所有按钮的声音效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28281255/

相关文章:

ios - 如何在 SWIFT 中获取 MP3 比特率

windows-phone-7 - 在 WP7 XNA 中单击 SoundEffect 播放的开始和结束

java - DOM 解析器在纯 HTML RSS 帖子上接收 NullPointerException

android - 为 MediaStore.ACTION_VIDEO_CAPTURE 设置最大持续时间的稳定方法

audio - Openvidu-将视频通话音频设置为扬声器Android

c# - 播放脚本中的音频源

iphone - 当 iPhone 音量完全调低至静音时,音频仍会播放

android - 错误 :(280, 61) 找不到错误 : cannot access zza class file for com. google.android.gms.common.internal.safeparcel.zza

android - Android上的Chrome在拒绝来电后切换音频输出

algorithm - 实时心律检测