一、录制工具类
public class MediaRecordingUtils {
private static final StringTAG ="MediaRecordingUtils";
private StringfilePath;
private Filefile;
private long startTime;
private long endTime;
private int BASE =1;
private int SPACE =100;
private MediaRecordermMediaRecorder;
private static final int MAX_LENGTH =1000 *60 *200;// 最大录音时长,单位毫秒,1000*60*10;
private OnAudioStatusUpdateListeneraudioStatusUpdateListener;
private final HandlermHandler =new Handler();
private RunnablemUpdateMicStatusTimer =new Runnable() {
public void run() {
updateMicStatus();
}
};
private boolean isRecording =false;
/**
* @param audioStatusUpdateListener
*/
public MediaRecordingUtils(OnAudioStatusUpdateListener audioStatusUpdateListener) {
this.audioStatusUpdateListener = audioStatusUpdateListener;
}
/**
* @return 开始录音 使用aac格式
*/
public void startRecord(String path) {
if (mMediaRecorder ==null)
mMediaRecorder =new MediaRecorder();
try {
file =new File(path +getTimeFolder()+getData() +".aac");
this.filePath =file.getPath();
Log.e(TAG,"开始录音");
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setOutputFile(file);//设置录音文件输出
mMediaRecorder.setMaxDuration(MAX_LENGTH);
mMediaRecorder.prepare();
mMediaRecorder.start();
isRecording =true;
startTime = System.currentTimeMillis();
updateMicStatus();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 停止录音
*/
public long stopRecord() {
isRecording =false;
if (mMediaRecorder ==null)
return 0L;
endTime = System.currentTimeMillis();
Log.e(TAG,"停止录音");
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder =null;
audioStatusUpdateListener.onStop(filePath);
filePath ="";
}catch (RuntimeException e) {
try {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder =null;
deleteFile(filePath);
}catch (Exception e1) {
e1.printStackTrace();
}
}
return endTime -startTime;
}
/**
* 取消录音
*/
public void cancelRecord() {
Log.e(TAG,"取消录音");
isRecording =false;
try {
if (mMediaRecorder !=null) {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder =null;
}
}catch (RuntimeException e) {
if (mMediaRecorder !=null) {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder =null;
}
}
deleteFile(filePath);
}
/**
* 更新麦克状态
*/
private void updateMicStatus() {
if (mMediaRecorder !=null) {
double ratio = (double)mMediaRecorder.getMaxAmplitude() /BASE;
double db =0;// 分贝
if (ratio >1) {
db =20 * Math.log10(ratio);
if (null !=audioStatusUpdateListener) {
audioStatusUpdateListener.onUpdate(db, System.currentTimeMillis() -startTime);
}
}
mHandler.postDelayed(mUpdateMicStatusTimer,SPACE);
}
}
//获取时间戳
private static String getData() {
Date date =new Date();
SimpleDateFormat dateFormat =new SimpleDateFormat("YYYYMMdd_HH_mm_ss");
String time = dateFormat.format(date);
return time;
}
/**
* @param path 删除文件
*/
private void deleteFile(String path) {
try {
if (path !=null) {
File file =new File(path);
if (file.exists()) {
file.delete();
}
filePath ="";
}
}catch (Exception e) {
e.printStackTrace();
}
}
//存储视频时按照当天日期分文件夹进行存储
private String getTimeFolder() {
Date date =new Date();
SimpleDateFormat dateFormat =new SimpleDateFormat("YYYYMMdd");
String time = dateFormat.format(date);
boolean orExistsDir = FileUtils.createOrExistsDir(Constants.audioPath + time +"/");
if (orExistsDir) {
return time +"/";
}else {
return "";
}
}
//是否正在录音
public boolean isRecording() {
return isRecording;
}
//录音状态回调接口
public interface OnAudioStatusUpdateListener {
/**
* @param db 当前声音分贝
* @param time 录音时长
*/
public void onUpdate(double db,long time);
/**
* @param filePath 停止录音 保存路径
*/
public void onStop(String filePath);
}
}