0

さまざまなボタンがあり、それぞれに独自の効果音があります。効果音を有効にするために、このクラスを使用しました:

enter code here

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's name).setSoundEffectsEnabled(false);

まったく機能しません。どうしたの?

4

0 に答える 0