huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.DeviceTest.helper;
 
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
 
import com.DeviceTest.DeviceTest;
 
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
 
public class Recorder implements MediaPlayer.OnCompletionListener,
       MediaPlayer.OnErrorListener {
   public static final int IDLE_STATE = 0;
   public static final int INTERNAL_ERROR = 2;
   public static final int NO_ERROR = 0;
   public static final int PLAYING_STATE = 2;
   public static final int RECORDING_STATE = 1;
   static final String SAMPLE_LENGTH_KEY = "sample_length";
   static final String SAMPLE_PATH_KEY = "sample_path";
   static final String SAMPLE_PREFIX = "recording";
   private static final String TEST_FILE_PATH = DeviceTest.DATA_PATH + "test";
   public static final int SDCARD_ACCESS_ERROR = 1;
   OnStateChangedListener mOnStateChangedListener = null;
   MediaPlayer mPlayer = null;
   MediaRecorder mRecorder = null;
   File mSampleFile = null;
   int mSampleLength;
   long mSampleStart = 0L;
   int mState;
 
   private void setError(int paramInt) {
       if (this.mOnStateChangedListener == null) {
           return;
       }
 
       this.mOnStateChangedListener.onError(paramInt);
   }
 
   private void setState(int state) {
       if (this.state() == state) {
           return;
 
       }
 
       this.mState = state;
 
       signalStateChanged(this.mState);
 
   }
 
   private void signalStateChanged(int paramInt) {
       if (this.mOnStateChangedListener == null)
           return;
       this.mOnStateChangedListener.onStateChanged(paramInt);
   }
 
   public void clear() {
       stop();
       this.mSampleLength = 0;
       signalStateChanged(0);
   }
 
   public void delete() {
       int i = 0;
       stop();
       if (this.mSampleFile != null)
           this.mSampleFile.delete();
       this.mSampleFile = null;
       this.mSampleLength = i;
       signalStateChanged(i);
   }
 
 
     public int getMaxAmplitude()
     {
            if (this.mState == Recorder.RECORDING_STATE) {
                return this.mRecorder.getMaxAmplitude();
            }
            return 0;
        }
 
   public void onCompletion(MediaPlayer paramMediaPlayer) {
       stop();
   }
 
   public boolean onError(MediaPlayer paramMediaPlayer, int paramInt1,
           int paramInt2) {
       stop();
       setError(1);
       return true;
   }
 
   public int progress() {
       if (this.state() != Recorder.PLAYING_STATE) {
           return 0;
       }
       long currentTime = System.currentTimeMillis();
 
       return (int) ((currentTime - this.mSampleStart) / 1000L);
 
   }
 
   public File sampleFile() {
       return this.mSampleFile;
   }
 
   public int sampleLength() {
       return this.mSampleLength;
   }
 
   public void setOnStateChangedListener(
           OnStateChangedListener paramOnStateChangedListener) {
       this.mOnStateChangedListener = paramOnStateChangedListener;
   }
 
   public void startPlayback() {
 
       stop();
 
       this.mPlayer = new MediaPlayer();
       try {
           FileInputStream fis = new FileInputStream(mSampleFile);
           this.mPlayer.setDataSource(fis.getFD());
           this.mPlayer.setOnCompletionListener(this);
           this.mPlayer.setOnErrorListener(this);
           this.mPlayer.prepare();
           this.mPlayer.start();
 
           this.mSampleStart = System.currentTimeMillis();
           setState(Recorder.PLAYING_STATE);
 
       } catch (Exception e) {
           setError(Recorder.PLAYING_STATE);
           setState(Recorder.IDLE_STATE);
 
       }
   }
 
   public void startRecording(int paramInt, String paramString) {
 
       stop();
       try {
           Log.i("Jeffy","Create new file:" + TEST_FILE_PATH);
           this.mSampleFile = new File(TEST_FILE_PATH);
           this.mSampleFile.createNewFile();
           Log.i("Jeffy","new file created,now create recorder");
           this.mRecorder = new MediaRecorder();
           this.mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
           this.mRecorder.setOutputFormat(paramInt);
           this.mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 
           String str = this.mSampleFile.getAbsolutePath();
           mRecorder.setOutputFile(str);
           Log.i("Jeffy","start to record");
           
           this.mRecorder.prepare();
           this.mRecorder.start();
 
           this.mSampleStart = System.currentTimeMillis();
           setState(Recorder.RECORDING_STATE);
       } catch (Exception e) {
           e.printStackTrace();
           setError(Recorder.IDLE_STATE);
           if(mRecorder != null) {
               this.mRecorder.reset();
               this.mRecorder.release();
               this.mRecorder = null;
           }
 
       }
 
   }
 
   public int state() {
       return this.mState;
   }
 
   public void stop() {
       stopRecording();
       stopPlayback();
   }
 
   public void stopPlayback() {
       if (this.mPlayer == null) {
           return;
       }
       this.mPlayer.stop();
       this.mPlayer.release();
       this.mPlayer = null;
       setState(Recorder.IDLE_STATE);
 
   }
 
   public void stopRecording() {
       if (this.mRecorder == null) {
           return;
       }
       this.mRecorder.stop();
       this.mRecorder.release();
       this.mRecorder = null;
 
       this.mSampleLength = (int) ((System.currentTimeMillis() - mSampleStart) / 1000L);
       setState(Recorder.IDLE_STATE);
   }
}
 
abstract interface OnStateChangedListener {
   public abstract void onError(int paramInt);
 
   public abstract void onStateChanged(int paramInt);
}