- 使用AudioRecord进行录音
- 实现pcm、wav、mp3音频的录制
- 实时获取录音的音量、及录音byte数据
- 获取wav/mp3录音文件的时长
- 可配置录音的采样率、位宽 (v1.04更新)
- 录音可视化 (v1.05更新)
- 音源支持内录(Android10及以上版本支持) (v1.09更新)
https://www.jianshu.com/p/c0222de2faed
dependencies {
implementation 'com.github.zhaolewei:ZlwAudioRecorder:v1.09'
}
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- 初始化
-
init
/** * 参数1: Application 实例 * 参数2: 是否打印日志 */ RecordManager.getInstance().init(MyApp.getInstance(), false);
-
在清单文件中注册Services
<service android:name="com.zlw.main.recorderlib.recorder.RecordService" />
-
确保有录音权限
- 配置录音参数
-
修改录音格式(默认:WAV)
RecordManager.getInstance().changeFormat(RecordConfig.RecordFormat.WAV);
-
修改录音配置
RecordManager.getInstance().changeRecordConfig(recordManager.getRecordConfig().setSampleRate(16000)); RecordManager.getInstance().changeRecordConfig(recordManager.getRecordConfig().setEncodingConfig(AudioFormat.ENCODING_PCM_8BIT));
-
修改录音音源
RecordManager.getInstance().setSource(RecordConfig.SOURCE_MIC); // 麦克风 RecordManager.getInstance().setSource(RecordConfig.SOURCE_SYSTEM); //系统内录
-
修改录音文件存放位置(默认sdcard/Record)
RecordManager.getInstance().changeRecordDir(recordDir);
-
录音状态监听
RecordManager.getInstance().setRecordStateListener(new RecordStateListener() { @Override public void onStateChange(RecordHelper.RecordState state) { } } @Override public void onError(String error) { } });
-
录音结果监听
RecordManager.getInstance().setRecordResultListener(new RecordResultListener() { @Override public void onResult(File result) { } });
-
声音大小监听
RecordManager.getInstance().setRecordSoundSizeListener(new RecordSoundSizeListener() { @Override public void onSoundSize(int soundSize) { } });
-
音频数据监听
recordManager.setRecordDataListener(new RecordDataListener() { @Override public void onData(byte[] data) { } });
-
音频可视化数据监听
recordManager.setRecordFftDataListener(new RecordFftDataListener() { @Override public void onFftData(byte[] data) { audioView.setWaveData(data); } });
- 录音控制
- 开始录音
RecordManager.getInstance().start();
- 暂停录音
RecordManager.getInstance().pasue();
- 恢复录音
RecordManager.getInstance().resume();
- 停止
RecordManager.getInstance().stop();