44import android .content .Context ;
55import android .os .Bundle ;
66import android .os .Handler ;
7+ import android .os .Looper ;
78import android .os .Message ;
89import android .util .Log ;
910import android .view .LayoutInflater ;
@@ -52,14 +53,12 @@ public class PreCallTest extends BaseFragment implements View.OnClickListener {
5253 private TextView lastmileQuality , lastmileResult ;
5354 private static final Integer MAX_COUNT_DOWN = 8 ;
5455 private int num ;
55- private Timer timer ;
56- private TimerTask task ;
5756 private boolean echoTesting = false ;
57+ private Thread countDownThread ;
5858
5959 @ Override
6060 public void onCreate (@ Nullable Bundle savedInstanceState ) {
6161 super .onCreate (savedInstanceState );
62- handler = new Handler ();
6362 }
6463
6564 @ Nullable
@@ -129,10 +128,9 @@ public void onClick(View v) {
129128 num = 0 ;
130129 engine .startEchoTest (MAX_COUNT_DOWN );
131130 btn_echo .setEnabled (false );
132- btn_echo .setText ("Recording on Microphone ..." );
133- timer = new Timer (false );
134- task = new TimerProcess ();
135- timer .schedule (task , 1000 , 1000 );
131+ btn_echo .setText (R .string .recording_start );
132+ countDownThread = new Thread (new CountDownTask ());
133+ countDownThread .start ();
136134 } else if (v .getId () == R .id .btn_echoVideoTest ) {
137135 if (!echoTesting ) {
138136 SurfaceView surfaceView = RtcEngine .CreateRendererView (getContext ());
@@ -148,8 +146,7 @@ public void onClick(View v) {
148146 btn_echoVideo .setText (getText (R .string .stop_echo_video_audio_test ));
149147 } else {
150148 engine .stopEchoTest ();
151- if (preview .getChildCount () > 0 )
152- {
149+ if (preview .getChildCount () > 0 ) {
153150 preview .removeAllViews ();
154151 }
155152 echoTesting = false ;
@@ -306,22 +303,63 @@ private void updateLastMileResult() {
306303 });
307304 }
308305
309- class TimerProcess extends TimerTask {
310- @ Override
311- public void run () {
312- num ++;
313- if (num >= MAX_COUNT_DOWN * 2 ) {
306+
307+ @ Override
308+ public void onDestroy () {
309+ /**leaveChannel and Destroy the RtcEngine instance*/
310+ if (engine != null ) {
311+ /**After joining a channel, the user must call the leaveChannel method to end the
312+ * call before joining another channel. This method returns 0 if the user leaves the
313+ * channel and releases all resources related to the call. This method call is
314+ * asynchronous, and the user has not exited the channel when the method call returns.
315+ * Once the user leaves the channel, the SDK triggers the onLeaveChannel callback.
316+ * A successful leaveChannel method call triggers the following callbacks:
317+ * 1:The local client: onLeaveChannel.
318+ * 2:The remote client: onUserOffline, if the user leaving the channel is in the
319+ * Communication channel, or is a BROADCASTER in the Live Broadcast profile.
320+ * @returns 0: Success.
321+ * < 0: Failure.
322+ * PS:
323+ * 1:If you call the destroy method immediately after calling the leaveChannel
324+ * method, the leaveChannel process interrupts, and the SDK does not trigger
325+ * the onLeaveChannel callback.
326+ * 2:If you call the leaveChannel method during CDN live streaming, the SDK
327+ * triggers the removeInjectStreamUrl method.*/
328+ engine .leaveChannel ();
329+ }
330+ handler .post (RtcEngine ::destroy );
331+ engine = null ;
332+ super .onDestroy ();
333+ }
334+
335+ class CountDownTask implements Runnable {
336+
337+ private void updateCountDown () {
338+ if (num > MAX_COUNT_DOWN * 2 )
339+ return ;
340+ try {
341+ Thread .sleep (1000 );
342+ } catch (InterruptedException e ) {
343+ e .printStackTrace ();
344+ }
345+ if (num == MAX_COUNT_DOWN * 2 ) {
314346 handler .post (() -> {
315- btn_echo .setEnabled (true );
316347 btn_echo .setText (getString (R .string .start_echo_audio_test ));
348+ btn_echo .setEnabled (true );
317349 });
318350 engine .stopEchoTest ();
319- task .cancel ();
320351 } else if (num >= MAX_COUNT_DOWN ) {
321- handler .post (() -> btn_echo .setText ("PLaying with " + (MAX_COUNT_DOWN * 2 - num ) + "Seconds" ));
352+ handler .post (() -> btn_echo .setText (getString ( R . string . echo_playing_countdown ) + " " + (MAX_COUNT_DOWN * 2 - num + 1 ) ));
322353 } else {
323- handler .post (() -> btn_echo .setText ("Recording with " + (MAX_COUNT_DOWN - num ) + "Seconds" ));
354+ handler .post (() -> btn_echo .setText (getString ( R . string . echo_record_countdown ) + " " + (MAX_COUNT_DOWN - num + 1 ) ));
324355 }
356+ num ++;
357+ updateCountDown ();
358+ }
359+
360+ @ Override
361+ public void run () {
362+ updateCountDown ();
325363 }
326364 }
327365
0 commit comments