在我的Android应用程序中,我需要录制音频仅1分钟?任何人都可以帮我录制一段时间的音频吗?我写了一个录制音频的代码,它只会在按下停止按钮时停止,但我需要在1分钟后自动停止录制...
这里是我的代码..但它不起作用
try {
recorder.prepare();
recorder.start();
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AudioRecordingActivity.this, "record successfullly ",
Toast.LENGTH_SHORT).show();
i1=i1+1;
}
});
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
10*1000);
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
Toast.makeText(AudioRecordingActivity.this, "record successfullly",
Toast.LENGTH_SHORT).show();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
尝试以下代码
recorder.start();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recorder.stop();
}
}, 60000);
答案 1 :(得分:0)
使用以下代码。
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
});
}
}, 60000);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
试试recorder.setMaxDuration(60*1000);
或者,如果您需要重复执行此操作,请使用上面的答案,但添加
if (null == recorder)
{
recorder = new MediaRecorder();
} else recorder.reset();
这种方法更有可能:)