重复音频文件,并在任务更改时停止

时间:2017-11-17 14:20:52

标签: java android delay repeat

我想重复播放音频文件,其中包含"搜索"每1分钟一次。当CASE_STATUS_SEARCHING将更改为其他文件时,如何实现它以停止此文件?我使用了TIMERSOUNDPOOL

public class Player { 
private static SoundPool soundPool;
private static int searching;
final Timer timer = new Timer();


public void searching () {
    timer.schedule(new TimerTask() {
                       @Override
                       public void run() {
                           soundPool.play(searching, 1.0f, 1.0f, 1, 0, 1.0f);
                       }
                   }, 60000);
}

//我在这里开始线程:

public class Locator BroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
final String action = intent.getAction();

switch (newStatus) {
    case STATUS_SEARCHING:
                        stateStr = getString(R.string.Scanning);
                        tvStatus.setText(R.string.Scanning);
                        Player.searching();
                        break;
}

1 个答案:

答案 0 :(得分:0)

我在stackoverflow的帮助下自己解决了这个问题。

public void playStillSearching() {

    if (handler == null) {
            handler = new Handler();
            handler.postDelayed(runnable = new Runnable() {
                @Override
                public void run() {
                    soundPool.play(stillSearching, 1.0f, 1.0f, 1, 0, 1.0f);
                    handler.postDelayed(this, 3000);
                }
            }, 3000);
    }
}

public void stopSpeak() {

    if (handler != null) {
        handler.removeCallbacks(runnable);
    }
}

比我实现我的活动。