2015-02-02 7 views
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); 

내 버튼의 내 온 클릭에

는 :

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

이 제대로 작동합니다. 하지만 지금은 내 모든 버튼의 사운드를 사용 중지하는 것과 같은 다른 버튼을 사용하고 싶습니다.

button(my_button_name).setSoundEffectsEnabled(false); 

을했지만 작동하지 않습니다 : 내 버튼을 클릭하면이 코드를 사용 (해제). 무엇이 문제입니까?

+0

내 대답을 확인하십시오. 제대로 작동하지 않으면 알려주세요. 정답으로 표시해야한다면 그것을 업데이트 할 것입니다. ... :) –

답변

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)를 호출하여