Skip to content

Commit 55a1730

Browse files
committed
统一整理修改实现
1 parent 57d1d9a commit 55a1730

2 files changed

Lines changed: 28 additions & 38 deletions

File tree

library/src/main/java/com/czt/mp3recorder/DataEncodeThread.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.media.AudioRecord;
44
import android.os.Handler;
5+
import android.os.HandlerThread;
56
import android.os.Looper;
67
import android.os.Message;
78

@@ -11,44 +12,35 @@
1112
import java.io.FileNotFoundException;
1213
import java.io.FileOutputStream;
1314
import java.io.IOException;
14-
import java.lang.ref.WeakReference;
1515
import java.util.ArrayList;
1616
import java.util.Collections;
1717
import java.util.List;
18-
import java.util.concurrent.CountDownLatch;
1918

20-
public class DataEncodeThread extends Thread implements AudioRecord.OnRecordPositionUpdateListener {
21-
public static final int PROCESS_STOP = 1;
19+
public class DataEncodeThread extends HandlerThread implements AudioRecord.OnRecordPositionUpdateListener {
2220
private StopHandler mHandler;
21+
private static final int PROCESS_STOP = 1;
2322
private byte[] mMp3Buffer;
2423
private FileOutputStream mFileOutputStream;
2524

26-
private CountDownLatch mHandlerInitLatch = new CountDownLatch(1);
27-
28-
/**
29-
* @see <a>https://groups.google.com/forum/?fromgroups=#!msg/android-developers/1aPZXZG6kWk/lIYDavGYn5UJ</a>
30-
* @author buihong_ha
31-
*/
32-
static class StopHandler extends Handler {
25+
private static class StopHandler extends Handler {
3326

34-
WeakReference<DataEncodeThread> encodeThread;
27+
private DataEncodeThread encodeThread;
3528

36-
public StopHandler(DataEncodeThread encodeThread) {
37-
this.encodeThread = new WeakReference<>(encodeThread);
29+
public StopHandler(Looper looper, DataEncodeThread encodeThread) {
30+
super(looper);
31+
this.encodeThread = encodeThread;
3832
}
39-
33+
4034
@Override
4135
public void handleMessage(Message msg) {
4236
if (msg.what == PROCESS_STOP) {
43-
DataEncodeThread threadRef = encodeThread.get();
4437
//处理缓冲区中的数据
45-
while (threadRef.processData() > 0);
38+
while (encodeThread.processData() > 0);
4639
// Cancel any event left in the queue
47-
removeCallbacksAndMessages(null);
48-
threadRef.flushAndRelease();
40+
removeCallbacksAndMessages(null);
41+
encodeThread.flushAndRelease();
4942
getLooper().quit();
5043
}
51-
super.handleMessage(msg);
5244
}
5345
}
5446

@@ -59,28 +51,29 @@ public void handleMessage(Message msg) {
5951
* @throws FileNotFoundException file not found
6052
*/
6153
public DataEncodeThread(File file, int bufferSize) throws FileNotFoundException {
54+
super("DataEncodeThread");
6255
this.mFileOutputStream = new FileOutputStream(file);
6356
mMp3Buffer = new byte[(int) (7200 + (bufferSize * 2 * 1.25))];
6457
}
6558

6659
@Override
67-
public void run() {
68-
Looper.prepare();
69-
mHandler = new StopHandler(this);
70-
mHandlerInitLatch.countDown();
71-
Looper.loop();
60+
public synchronized void start() {
61+
super.start();
62+
mHandler = new StopHandler(getLooper(), this);
7263
}
7364

74-
/**
75-
* Return the handler attach to this thread
76-
* @return the handler attach to this thread
77-
*/
78-
public Handler getHandler() {
79-
try {
80-
mHandlerInitLatch.await();
81-
} catch (InterruptedException e) {
82-
e.printStackTrace();
65+
private void check() {
66+
if (mHandler == null) {
67+
throw new IllegalStateException();
8368
}
69+
}
70+
71+
public void sendStopMessage() {
72+
check();
73+
mHandler.sendEmptyMessage(PROCESS_STOP);
74+
}
75+
public Handler getHandler() {
76+
check();
8477
return mHandler;
8578
}
8679

library/src/main/java/com/czt/mp3recorder/MP3Recorder.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.media.AudioFormat;
44
import android.media.AudioRecord;
55
import android.media.MediaRecorder;
6-
import android.os.Message;
76

87
import com.czt.mp3recorder.util.LameUtil;
98

@@ -87,9 +86,7 @@ public void run() {
8786
mAudioRecord = null;
8887
// stop the encoding thread and try to wait
8988
// until the thread finishes its job
90-
Message msg = Message.obtain(mEncodeThread.getHandler(),
91-
DataEncodeThread.PROCESS_STOP);
92-
msg.sendToTarget();
89+
mEncodeThread.sendStopMessage();
9390
}
9491
/**
9592
* 此计算方法来自samsung开发范例

0 commit comments

Comments
 (0)