jason white
jason white

Reputation: 711

Android's Wait function

I have a snippet of Android code, My understanding is a new thread is created in the background which starts the record function. It waits for 10seconds But my question how to stop the recording. From the record function it stops when the boolean isRecording is false.

My question is isRecording turns false after 10 seconds?

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Thread thread = new Thread(new Runnable() {
        public void run() {
            isRecording=true;
        record();              //starts to record
        } 
        });
        thread.start();   //thread start


     //=============================================//

        synchronized (this) 
        {

     try {
     wait(10000);

                  //wait for 10second
    } catch (InterruptedException e) {}
        }
    isRecording = false;   

    try {
      thread.join();  //blocks the current thread until the receiver finish execution and dies.
   } catch (InterruptedException e) {}     //interruptedException

   }
  }


   public void record() {


        short[] buffer = new short[bufferSize]; 
        audioRecord.startRecording();

        while (isRecording) {
            int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
            for (int i = 0; i < bufferReadResult; i++)
            dos.writeShort(buffer[i]);
            }

        audioRecord.stop();
        dos.close();

        } catch (Throwable t) {
        Log.e("AudioRecord","Recording Failed");
        }

}

Upvotes: 0

Views: 1935

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

Use CountDownTimer:

new CountDownTimer(10000, // 10 second countdown
9999) // onTick time, not used 
{

     public void onTick(long millisUntilFinished) {
         // Not used
     }

     public void onFinish() {
         isRecording = false;
     }
}.start();

Upvotes: 1

Related Questions