我有一段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");
}
}