我有一段Android代码,我的理解是在后台创建一个新的线程来启动记录功能。等待10秒 但我的问题是如何停止录音。从记录函数中,当布尔值isRecording为false时,它将停止。
我的问题是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");
}
}
答案 0 :(得分:1)
new CountDownTimer(10000, // 10 second countdown
9999) // onTick time, not used
{
public void onTick(long millisUntilFinished) {
// Not used
}
public void onFinish() {
isRecording = false;
}
}.start();