【问题标题】:how to disable my sound effect for my all the buttons?如何禁用我所有按钮的音效?
【发布时间】:2015-02-03 00:13:18
【问题描述】:

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

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

    }
    ...
}

然后在我的活动中,我使用此代码来识别我的班级以应用声音。

Effects.getInstance().init(this);

在我的按钮上点击:

Effects.getInstance().playSound(Effects.SOUND_1);

它工作正常。 但现在我想要另一个按钮,比如禁用来禁用我所有按钮的声音。当我单击按钮(禁用)时,我使用了以下代码:

 button(my_button_name).setSoundEffectsEnabled(false);

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

【问题讨论】:

  • 检查我的答案让我知道它是否有效,否则我会更新它,如果有效,您必须将其标记为正确答案... :)

标签: android audio soundeffect


【解决方案1】:

添加您的Effects 类布尔变量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)禁用或启用音效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多