0

ボタンごとに効果音が付いています。効果音を設定するには、このクラスを使用しました:

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

そして私のボタンの私のonclickで:

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

それは正しく動作します。しかし、今は無効のような別のボタンが必要で、すべてのボタンの音を無効にします。ボタン(無効)をクリックすると、次のコードを使用しました:

 button(my_button_name).setSoundEffectsEnabled(false);

しかし、うまくいきません。どうしたの?

4

1 に答える 1

0

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)

于 2015-02-02T16:07:50.913 に答える