22
33import android .media .AudioRecord ;
44import android .os .Handler ;
5+ import android .os .HandlerThread ;
56import android .os .Looper ;
67import android .os .Message ;
78
1112import java .io .FileNotFoundException ;
1213import java .io .FileOutputStream ;
1314import java .io .IOException ;
14- import java .lang .ref .WeakReference ;
1515import java .util .ArrayList ;
1616import java .util .Collections ;
1717import 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
0 commit comments