我正在使用MediaRecorder构建录音机,我想重命名创建的文件
所以我的主要步骤是:
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats = MediaRecorder.OutputFormat.MPEG_4;
private String file_exts = AUDIO_RECORDER_FILE_EXT_MP4 ;
private String getFilename() {
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() +
file_exts);
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats(currentFormat));
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
}
并在AudioRecorder文件夹中创建名称为“138672.mp4”
那么,如何更改它,我想用名称a.mp4(示例)
创建它答案 0 :(得分:0)
您可以使用此代码代替getFilename()方法:
private String getFilename() {
File file = new File(Environment.getExternalStorageDirectory(), "a.mp4");
if (!file.exists()) {
file.mkdirs();
}
在startRecording()方法中设置此代码:
recorder.setOutputFile(file+"");