简体   繁体   English

Android-如何在不停止的情况下在TimerTask中静音SoundPool

[英]Android - How to mute SoundPool in TimerTask without stopping it

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. 我希望能够在不停止TimerTask的情况下将其静音,因为我希望速率在取消静音后保持一致。 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. 但是,调用setMute(true)不起作用。 Does anyone know how I can mute my SoundPool? 有人知道我如何使SoundPool静音吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM