Search code examples
androidandroid-audiorecord

Android: 2 tasks one after the other with time difference


I am working on AudioRecord.

The requirement is I have to generate a tone and play the tone for 0.25 seconds. After that the audio recording should be automatically started and continue for 10 seconds. After 10 seconds, the audio recording should be stopped.

I wrote the code to generate tone and to record and play the audio.

But I could not sequence it with the required timing. I am facing thread issue because of the RecordThread: overflow or java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare().

genTone(Constants.HEART_TONE_FREQ);
playToneInPhone();

Thread recordThread = new Thread(new Runnable() {
    @Override
    public void run() {
        isRecording=true;
        recordingTimer = new RecordingTimer(10000,1000);
        recordingTimer.start();
        startRecording();
    }
});
try {
    recordThread.sleep(250);
    recordThread.start();
} catch(InterruptedException ie) {

}

RecordingTimer is a CountDownTimer. Can someone help me out resolving this issue?


Solution

  • I fixed the above issue by puttin recordingTimer after the thread start. Please let me know if there is any other better way of doing this.

    genTone(Constants.HEART_TONE_FREQ);
        playToneInPhone();
    
        Thread recordThread = new Thread(new Runnable() {
            @Override
            public void run() {
                isRecording=true;
    
                startRecording();
            }
        });
        try {
            recordThread.sleep(250);
            recordThread.start();
            recordingTimer = new RecordingTimer(10000,1000);
            recordingTimer.start();
        } catch(InterruptedException ie) {
    
        }