android中的线程计时

时间:2014-04-18 08:05:39

标签: android multithreading timer

我写了一个用于录制的android代码,其中包含一些线程。但我需要将线程执行时间限制为1秒。虽然这是我第一次尝试使用TimerTask的线程,如下所示:timerTask or handler

但它不起作用。

这是代码:

void playSound(){
        AudioTrack audioTrack= null;
        try{
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, AudioTrack.MODE_STATIC);
        audioTrack.write(generatedSnd, 0, generatedSnd.length);

        audioTrack.play();
        Thread thr=new Thread(new Runnable(){
            public void run(){
                startRecording();
            }
        });

        thr.start();
        timer.schedule(new TimerTask(){
            int n=0;
            public void run(){
                stopRecording();
            }
        },1,1000);
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
}

public void stopRecording()
{ mRecorder.stop();
mRecorder.release();

}

1 个答案:

答案 0 :(得分:0)

你致电schedule(TimerTask task, long delay, long period) - 这是一项重复的任务。 使用schedule(TimerTask task, long delay)

相关问题