1

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?

Pink Jazz
  • 784
  • 4
  • 13
  • 34

1 Answers1

0

Figured it out. It works when I use static methods and variables.

public class Metronome {
    public static boolean mute = false;
    public static boolean playing = false;
    private static Timer mainTimer;
    private static SoundPool soundPool;
    private static int soundID;

    public static void loadSound(Context context) {
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        soundID = soundPool.load(context, R.raw.metronome, 1);
    }

    public static void play(float pace, boolean isMuted) {
        mainTimer = new Timer();
        MyTimerTask mainTimerTask = new MyTimerTask();
        mainTimer.schedule(mainTimerTask, 0, Math.round(pace * 1000));
        mute = isMuted;
        playing = true;
    }

    public static void stop() {
        mainTimer.cancel();
        mainTimer.purge();
        playing = false;
    }

    static class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            playSound();
        }

        private void playSound() {
            if (!mute) {
                soundPool.play(soundID, 1, 1, 1, 0, 1);
            }
        }
    }
}
Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • Considering the official Android docs for SoundPool mention nothing about a global state, singletons or the like, I believe the real problem was somewhere else and changing everything to static just happened to fix it. For example, calling setMute() on a different instance of Metronome than the one playing the sound. – Thomas Leyk Feb 13 '19 at 19:08