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(); } }; }
相关文章推荐
- Android实战简易教程-第六十八枪(android小工具appwidget之时间显示)
- Android ProgressBar手动控制开始和停止
- 用as控制音效的播放、暂停、停止及时间显示
- Android中Toast显示时间的自定义控制
- Android实战简易教程-第五十九枪(EventBus小实例-传值、控制其他页控件显示)
- android MediaRecorder实现录音功能 显示时间 计时
- 网页播放器 windowsmediaplay中控制从wmv的指定时间开始播放,指定时间停止播放
- android桌面悬浮窗显示录屏时间控制效果
- Android实战简易教程<五十九>(EventBus小实例-传值、控制其他页控件显示)
- Android图片切片控制与显示案例实战
- android framework/base 看源码实战:控制Toast显示时长(提供在线看源码地址)
- 调用 显示控制面板 系统时间日期属性等方法
- [android] AlertDialog显示时间
- VC学习资料收集(17):实战VC时间控制函数
- 外中断(按键)控制数码管显示
- 实战VC时间控制函数
- 实战VC时间控制函数
- Android短信列表的时间显示
- ASP.NET Google Maps Javascript API V3 实战基础篇一叠加层显示控制
- Android 控制软键盘的显示与隐藏