一骚灸、錄制工具類
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);//設(shè)置錄音文件輸出
? ? ? ? ? ? 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);
}
/**
* 更新麥克狀態(tài)
*/
? ? 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();
}
}
//存儲視頻時按照當(dāng)天日期分文件夾進行存儲
? ? 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;
}
//錄音狀態(tài)回調(diào)接口
? ? public interface OnAudioStatusUpdateListener {
/**
? ? ? ? * @param db? 當(dāng)前聲音分貝
? ? ? ? * @param time 錄音時長
*/
? ? ? ? public void onUpdate(double db,long time);
/**
? ? ? ? * @param filePath 停止錄音 保存路徑
*/
? ? ? ? public void onStop(String filePath);
}
}