Android之录音工具类
16lz
2022-04-18
/** * 录音工具类 * * @author rendongwei * */public class RecordUtil { private static final int SAMPLE_RATE_IN_HZ = 8000; private MediaRecorder recorder = new MediaRecorder(); // 录音的路径 private String mPath; public RecordUtil(String path) { mPath = path; } /** * 开始录音 * * @throws IOException */ public void start() throws IOException { String state = android.os.Environment.getExternalStorageState(); if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) { throw new IOException("SD Card is not mounted,It is " + state + "."); } File directory = new File(mPath).getParentFile(); if (!directory.exists() && !directory.mkdirs()) { throw new IOException("Path to file could not be created"); } recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ); recorder.setOutputFile(mPath); recorder.prepare(); recorder.start(); } /** * 结束录音 * * @throws IOException */ public void stop() throws IOException { recorder.stop(); recorder.release(); } /** * 获取录音时间 * * @return */ public double getAmplitude() { if (recorder != null) { return (recorder.getMaxAmplitude()); } return 0; }}
更多相关文章
- android 获取通讯录中同一联系人的多个号码
- Android(安卓)各种音量的获取和设置
- 安卓各文件存储路径汇总(Android(安卓)file path)
- android过滤隐藏文件FileFilter
- Android乐动力V5.75最新获Key方法,提交步数,QQ登陆获取key案例
- Android中获取指定日期是星期几
- Android开发遇到的问题----自定义RecyclerView Adapter Item 显
- Android(安卓)中常见bug 总结
- Pycharm安装PyQt5的详细教程