I am trying to create a metronome sound, however, what isn't working is the ability to mute it. I would like the ability to mute it without stopping the TimerTask since I want the rate to be consistent once it is unmuted. Here is my code:
public class Metronome {
private boolean mute;
private boolean playing = false;
private Timer mainTimer;
private SoundPool mSoundPool;
int mSoundID;
public Metronome(Context context) {
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mSoundID = mSoundPool.load(context, R.raw.metronome, 1);
}
public void play(float pace, boolean mute) {
mainTimer = new Timer();
MyTimerTask mainTimerTask = new MyTimerTask();
mainTimer.schedule(mainTimerTask, 0, Math.round(pace * 1000));
this.mute = mute;
playing = true;
}
public void stop() {
mainTimer.cancel();
mainTimer.purge();
playing = false;
}
public boolean isPlaying() {
return playing;
}
public void setMute(boolean mute) {
this.mute = mute;
if (mute) {
mSoundPool.setVolume(mSoundID, 0, 0);
} else {
mSoundPool.setVolume(mSoundID, 1, 1);
}
}
private void playSound() {
if (!mute) {
mSoundPool.play(mSoundID, 1, 1, 1, 0, 1);
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
playSound();
}
}
}
However, calling setMute(true)
does not work. Does anyone know how I can mute my SoundPool?