您的位置:首页 > 移动开发 > Android开发

Android实战之按键控制录音开始停止取消,显示时间与声贝

2017-05-24 15:29 405 查看
/**
* Created by WAYNE on 2017/5/24.
*/

public class Recoder  {
private static final String TAG = "Recoder";
LinearLayout linearLayout;
//文件路径
private String filePath;
//文件夹路径
private String folderPath;
//录音的类
private MediaRecorder mMediaRecorder;
//最长录音时常
public static final int MAX_LENGTH = 10 * 60 * 1000;
//录音监听器
private OnAudioStatusUpdateListener audioStatusUpdateListener;
//开始时间
private long startTime;
//结束时间
private long endTime;

/**
* 构造方法
*
* @param context
* @param iConfig
*/
public Recoder(Context context, Config iConfig) throws IOException {
super(context, iConfig);
folderPath = Environment.getExternalStorageDirectory()
.getCanonicalPath().toString()
+ "/" + ResUtils.getValue(getContext(), R.string.app_name) + "/";
File path = new File(folderPath);
if (!path.exists()) {
path.mkdirs();
}
}

@Override
protected View buildElement() throws ClassNotFoundException {
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//        if (getCommand().containsKey("hoding")) {
//
//        } else if (getCommand().containsKey("click")) {
Button buttonStart = new Button(getContext());
buttonStart.setText("开始");
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecord();
}
});

Button buttonStop = new Button(getContext());
buttonStop.setText("停止");
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecord();
}
});

Button buttonCancel = new Button(getContext());
buttonCancel.setText("取消");
buttonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelRecord();
}
});

final TextView textView = new TextView(getContext());
textView.setText("点击开始按钮开始录音");
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);

this.setAudioStatusUpdateListener(new OnAudioStatusUpdateListener() {
@Override
public void onStart(long time) {
textView.setText("开始录音");
}

@Override
public void onUpdate(int db, long time) {
long min = time / (1000 * 60);
long sec = (time / 1000) % 60;
textView.setText("已录:" + min + ":" + sec + ",声贝:" + db);
}

@Override
public void onStop(String filePath) {
textView.setText("已保存");
Toast.makeText(getContext(), "录音文件已保存至" + filePath, Toast.LENGTH_SHORT).show();
}
});

linearLayout.addView(buttonStart);
linearLayout.addView(buttonStop);
linearLayout.addView(buttonCancel);
linearLayout.addView(textView);
//        }
return linearLayout;
}

/**
* 开始录音 使用amr格式
* 录音文件
*
* @return
*/
public void startRecord() {
// 开始录音
// 实例化录音类
if (mMediaRecorder == null)
mMediaRecorder = new MediaRecorder();
try {
//声音来源,这里设置为麦克风,还有好多种 看不懂
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//声音输出格式,使用安卓默认的AMR吧
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//编码格式
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//输出的文件名
filePath = folderPath + System.currentTimeMillis() + ".amr";
//文件输出地址
mMediaRecorder.setOutputFile(filePath);
//设置允许最长的录音长度
mMediaRecorder.setMaxDuration(MAX_LENGTH);
//准备
mMediaRecorder.prepare();
//这个方法开始录制了
mMediaRecorder.start();
/* 获取开始时间* */
startTime = System.currentTimeMillis();
//这里做一些开始录音后要做的事,比如更新UI 比如开始计时
audioStatusUpdateListener.onStart(startTime);
//开始不断刷新UI
updateMicStatus();
} catch (IllegalStateException e) {
Log.i(TAG, "IllegalStateException" + e.getMessage());
} catch (IOException e) {
Log.i(TAG, "IOException" + e.getMessage());
}
}

/**
* 停止录音
*/
public long stopRecord() {
if (mMediaRecorder == null)
return 0L;
endTime = System.currentTimeMillis();
try {
//停止 重置 释放资源
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;

audioStatusUpdateListener.onStop(filePath);
filePath = "";
//据说部分手机会出错 尝试捕获
} catch (RuntimeException e) {
Log.d(TAG, "stopRecord: " + e.getMessage());
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;

} catch (RuntimeException e) {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
}
File file = new File(filePath);
if (file.exists())
file.delete();

filePath = "";

}

public void setAudioStatusUpdateListener(OnAudioStatusUpdateListener audioStatusUpdateListener) {
this.audioStatusUpdateListener = audioStatusUpdateListener;
}

public interface OnAudioStatusUpdateListener {
/**
* 开始录
*
* @param time 开始的时间
*/
public void onStart(long time);

/**
* 录音中...
*
* @param db   当前声音分贝
* @param time 录音时长
*/
public void onUpdate(int db, long time);

/**
* 停止录音
*
* @param filePath 保存路径
*/
public void onStop(String filePath);
}

private int delay = 100;// 间隔取样时间

private void updateMicStatus() {

if (mMediaRecorder != null) {
double ratio = (double) mMediaRecorder.getMaxAmplitude();
int db = 0;// 分贝
if (ratio > 1) {
db = (int) (20 * Math.log10(ratio));
if (null != audioStatusUpdateListener) {
audioStatusUpdateListener.onUpdate(db, System.currentTimeMillis() - startTime);
}
}
mHandler.postDelayed(mUpdateMicStatusTimer, delay);
}
}

private final Handler mHandler = new Handler();
private Runnable mUpdateMicStatusTimer = new Runnable() {
public void run() {
updateMicStatus();
}
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: