首先看一下页面,大概就是这个样子的,复杂的功能也是从简单的先实现,不要着急,慢慢来,重要的是你的思维
1.录音工具类
/**
* Author : 马占柱
* E-mail : mazhanzhu_3351@163.com
* Time : 2021/4/19 15:09
* Desc : 录音工具类
*/
public class AudioRecoderUtils {
public static final String TAG = "AudioRecoderUtils";
//文件路径
private String filePath;
private MediaRecorder mMediaRecorder;
public static final int MAX_LENGTH = 1000 * 60 * 10;// 最大录音时长1000*60*10;
private OnAudioStatusUpdateListener audioStatusUpdateListener;
/**
* 文件存储默认sdcard/record
*/
public AudioRecoderUtils() {
}
private long startTime;
private long endTime;
/**
* 开始录音 使用AAC格式(苹果安卓通用)
* 录音文件
*/
public void startRecord() {
// 开始录音
/* ①Initial:实例化MediaRecorder对象 */
if (mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder();
}
try {
/* ②setAudioSource/setVedioSource */
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
/* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default 声音的(波形)的采样 */
// mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
/*
* ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
* ,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
WAV格式:录音质量高,但是压缩率小,文件大
AAC格式:相对于mp3,AAC格式的音质更佳,文件更小;有损压缩;一般苹果或者Android SDK4.1.2(API 16)及以上版本支持播放
AMR格式:压缩比比较大,但相对其他的压缩格式质量比较差,多用于人声,通话录音
至于常用的mp3格式,使用MediaRecorder没有该视频格式输出。一些人的做法是使用AudioRecord录音,然后编码成wav格式,再转换成mp3格式
*/
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
/* 获取开始时间* */
startTime = System.currentTimeMillis();
filePath = Base64Util.Mzz_MP3 + "录音" + startTime + ".mp3";
/* ③准备 */
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.setMaxDuration(MAX_LENGTH);
mMediaRecorder.prepare();
/* ④开始 */
mMediaRecorder.start();
updateMicStatus();
Log_Ma.e(TAG, "startTime" + startTime);
} catch (IllegalStateException e) {
Log_Ma.e(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
} catch (IOException e) {
Log_Ma.e(TAG, "call startAmr(File mRecAudioFile) failed!" + e.getMessage());
}
}
/**
* 停止录音
*/
public long stopRecord() {
if (mMediaRecorder == null) {
return 0L;
}
endTime = System.currentTimeMillis();
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
long time = endTime - startTime;
audioStatusUpdateListener.onStop(filePath, time);
filePath = "";
} catch (RuntimeException e) {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
filePath = "";
}
return endTime - startTime;
}
/**
* 取消录音
*/
public void cancelRecord() {
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
audioStatusUpdateListener.onCancel();
} catch (RuntimeException e) {
mMediaRecorder = null;
e.printStackTrace();
}
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
filePath = "";
}
private final Handler mHandler = new Handler();
private Runnable mUpdateMicStatusTimer = new Runnable() {
@Override
public void run() {
updateMicStatus();
}
};
private int BASE = 1;
private int SPACE = 100;// 间隔取样时间
public void setOnAudioStatusUpdateListener(OnAudioStatusUpdateListener audioStatusUpdateListener) {
this.audioStatusUpdateListener = audioStatusUpdateListener;
}
/**
* 更新麦克状态
*/
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);
}
}
public interface OnAudioStatusUpdateListener {
/**
* 录音中...
*
* @param db 当前声音分贝
* @param time 录音时长
*/
void onUpdate(double db, long time);
/**
* 停止录音
*
* @param filePath 保存路径
* @param time
*/
void onStop(String filePath, long time);
/**
* 取消录音
*/
void onCancel();
}
}
页面没什么好说的了,就是正常的布局,有点意思的就是这里
这是record_microphone里面的代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@mipmap/record_top" />
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:drawable="@mipmap/record_bottom"
android:gravity="bottom" />
</item>
</layer-list>
这是图片资源
2.在activity中使用
在onCreate中new出来
mAudioRecoderUtils = new AudioRecoderUtils();
然后设置录音回调【注意!这里面mImageView这个值的设置,这就是上面截图里面的那个view的iD】
//录音回调
mAudioRecoderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {
public long time;
//录音中....db为声音分贝,time为录音时长
@Override
public void onUpdate(double db, long time) {
mImageView.getDrawable().setLevel((int) (3000 + 6000 * db / 100));
mTextView.setText(TimeUtil.long2String(time));
this.time = time;
}
//录音结束,filePath为保存路径
@Override
public void onStop(String filePath, long time) {
Log_Ma.e(TAG, "onStop:tiem " + time);
if (time / 1000 < 1) {
/*录音时长小鱼1秒不处理*/
ToastUtils.showToast("录音过短");
} else {
mTextView.setText(TimeUtil.long2String(0));
Bean_Me_Tab tab = new Bean_Me_Tab();
tab.setFilePath(filePath);
tab.setFileLeng((int) time / 1000);
music_lists.add(tab);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancel() {
mTextView.setText("0.00");
}
});
在你想要开始的地方调用 mAudioRecoderUtils.startRecord();
在你想要结束的地方调用 mAudioRecoderUtils.stopRecord();
完事了!大家可以愉快的玩耍了