android 录音文件,Android开发中,MediaRecorder录制音频文件。

编程入门 行业动态 更新时间:2024-10-11 11:22:54

android 录音文件,Android开发中,MediaRecorder录制<a href=https://www.elefans.com/category/jswz/34/1771399.html style=音频文件。"/>

android 录音文件,Android开发中,MediaRecorder录制音频文件。

一、录制工具类

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);

}

}

二、根据不同需求,自定义调用逻辑即可,注意容错性处理。

更多推荐

android 录音文件,Android开发中,MediaRecorder录制音频文件。

本文发布于:2024-03-07 18:54:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:音频文件   文件   android   MediaRecorder   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!