我使用 MediaRecorder 创建了程序,我想显示音频记录的时间期限。MediaRecorder 只是用来录制声音。那如何显示时间呢?
在记录开始的 System.currentTimeMillis(); 方法中设置 start_time 方法。
然后使用 Runnable 可以执行一个循环,直到停止记录。
final Handler handler = new Handler();
Runnable update_runnable = new Runnable() {
public void run() {
updateTextView();
}
};
Then with the updateTextView() you'd do something like this:
long duration = (int) ((System.currentTimeMillis() - start_time) / 1000);
// ... set TextView with duration value
handler.postDelayed(update_runnable, 1000); /
我觉得在启动start()之后即开始记时操作
可开启个线程刷新时间记录
当点击停止就将记时的run里的while(iFlag)设为false就行了,不知是你想的意思不
注意模拟器上不方便测试,以下代码供参考
@Override //test code
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
System.out.println("---开始录音---");
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(RecordSoundDemoActivity.this, "SD卡不存在",
Toast.LENGTH_LONG).show();
return;
}
try {
mediaRecorder = new MediaRecorder();
// 创建音频输出路径
soundFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalPath() + File.separator + "sound.3gp");
// 设置录音的来源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置录制的声音输出格式
mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置声音的编码格式
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置录音的输出文件路径
mediaRecorder.setOutputFile(soundFile.getAbsolutePath());
// 做预期准备
mediaRecorder.prepare();
// 开始录音
mediaRecorder.start();
//start new thread here...
} catch (Exception e) {
// TODO: handle exception
}
break;
case R.id.btnStop:
System.out.println("---停止录音---");
if (soundFile != null && soundFile.exists()) {
mediaRecorder.stop();
// 释放资源
mediaRecorder.release();
mediaRecorder = null;
}
break;
default:
break;
}
}