0

我有一段Android代码,我的理解是在后台创建了一个新线程,它启动了记录功能。它等待 10 秒但我的问题是如何停止录制。当布尔值 isRecording 为假时,它从记录函数停止。

我的问题是 isRecording 在 10 秒后变为假?

   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");
        }

}
4

1 回答 1

1

使用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();
于 2013-03-03T01:22:26.323 回答