-
Notifications
You must be signed in to change notification settings - Fork 107
/
WaveMp3Recorder.java
176 lines (144 loc) · 4.45 KB
/
WaveMp3Recorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jaygoo.wavelineview;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import com.czt.mp3recorder.MP3Recorder;
import java.io.File;
import jaygoo.widget.wlv.WaveLineView;
/**
* ================================================
* 作 者:JayGoo
* 版 本:
* 创建日期:2017/7/18
* 描 述:
* ================================================
*/
public class WaveMp3Recorder extends WaveLineView {
private String RECORD_FILE_DIR = Environment.getExternalStorageDirectory()+"/";
private String recordFileName = "WaveLineViewTest.mp3";
private MP3Recorder mp3Recorder;
private long maxRecordTime = 1000 * 60 * 60 * 24;
private long recordTime = 0;
private int UPDATE_TIME = 200;
private OnRecordStateChangeListener mOnRecordStateChangeListener;
private Handler mHandler = new Handler();
public WaveMp3Recorder(Context context) {
this(context,null);
}
public WaveMp3Recorder(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public WaveMp3Recorder(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initRecorder();
}
private void updateRecordingUI(){
if (mp3Recorder != null) {
setVolume(100 * mp3Recorder.getVolume() / mp3Recorder.getMaxVolume());
}
}
private void updateStopRecordUI(){
stopAnim();
}
private void updateStartRecordUI(){
startAnim();
}
/**
* 录音更新进度条
*/
private Runnable mRecordProgressTask = new Runnable() {
public void run() {
//录音时间超出最大时间,自动停止
if (recordTime > maxRecordTime){
stopRecord(true);
}else {
updateRecordingUI();
if (mHandler != null) {
mHandler.postDelayed(mRecordProgressTask, UPDATE_TIME);
}
}
}
};
public void initRecorder(){
File recordFile = new File(RECORD_FILE_DIR, recordFileName);
mp3Recorder = new MP3Recorder(recordFile);
mp3Recorder.setDefaultLameMp3BitRate(96);
}
public void startRecording(){
try {
mp3Recorder.start();
if (mHandler != null) {
mHandler.post(mRecordProgressTask);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//停止录音按钮状态
public void stopRecord(boolean isFromUser){
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
try {
recordTime = 0;
updateStopRecordUI();
if (mp3Recorder != null) {
mp3Recorder.stop();
}
}catch (Exception e){
e.printStackTrace();
}
if (mOnRecordStateChangeListener != null){
mOnRecordStateChangeListener.onStopRecord(getRecordFile(),isFromUser);
}
}
public void stopRecord(){
stopRecord(false);
}
//开始录音
public void startRecord(){
updateStartRecordUI();
try {
mp3Recorder.start();
if (mHandler != null) {
mHandler.post(mRecordProgressTask);
}
} catch (Exception e) {
e.printStackTrace();
}
if (mOnRecordStateChangeListener != null){
mOnRecordStateChangeListener.onStartRecord();
}
}
//是否在录音
public boolean isRecording(){
if (mp3Recorder != null){
return mp3Recorder.isRecording();
}
return false;
}
//设置录音的最大时间
public void setMaxRecordTime(long millis){
maxRecordTime = millis;
}
//获取录音文件
public File getRecordFile(){
return new File(RECORD_FILE_DIR, recordFileName);
}
public String getRecordFilePath(){
return RECORD_FILE_DIR + "/"+recordFileName;
}
public MP3Recorder getMp3Recorder(){
return mp3Recorder;
}
//录音状态监听
public interface OnRecordStateChangeListener{
void onStartRecord();
void onStopRecord(File recordFile, boolean isFromUser);
}
public void setOnRecordStateChangeListener(OnRecordStateChangeListener listener){
mOnRecordStateChangeListener = listener;
}
}