如何循环handler.postdelayed只两次

时间:2018-01-02 14:43:47

标签: java android loops handler

我正在使用handler.postdelayed循环声音,延迟可由用户更改。但我希望声音只播放两次,然后停止。这是我到目前为止所拥有的,它无限循环:

eightHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    soundpool.play(drum, 1, 1, 1, 0, 0);
                    eightRunnable = this;
                    handler.postDelayed(eightRunnable, delay);
                }
            }, delay/2);

1 个答案:

答案 0 :(得分:1)

你可以这样做。

int i = 0;
eightHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    soundpool.play(drum, 1, 1, 1, 0, 0);
                    eightRunnable = this;
                    if(i < 2){
                        handler.postDelayed(eightRunnable, delay);
                    }
                    i++;
                }
            }, delay/2);