0

i wrote an android code for recording including some threads in it. but i need to limit the thread execution time to 1 second. though this is my first time with threads i tried using TimerTask as in here :timerTask or handler

but its not working.

here's the code:

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

}
Community
  • 1
  • 1
hanaa
  • 385
  • 6
  • 30
  • So the `stopRecording` is not called? – buptcoder Apr 18 '14 at 08:25
  • it is called, but doesn't stop. – hanaa Apr 18 '14 at 12:13
  • So I think you can do two things for this problem. 1. check the `stopRecording()`. 2. Try called `startRecording` and `stopRecording()` in the same thread. Because as you wrote, the two methods won't be called in the same thread. – buptcoder Apr 19 '14 at 09:00

1 Answers1

0

You call schedule(TimerTask task, long delay, long period) - it's a repeated task. Use schedule(TimerTask task, long delay)

zella
  • 4,645
  • 6
  • 35
  • 60