Skip to content

Commit 6cabbaa

Browse files
author
xianing
committed
optimize pre-call test timer task logic
1 parent da55c54 commit 6cabbaa

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/PreCallTest.java

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.os.Bundle;
66
import android.os.Handler;
7+
import android.os.Looper;
78
import android.os.Message;
89
import android.util.Log;
910
import 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

Android/APIExample/app/src/main/res/values-zh/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,7 @@
160160
<string name="stop_echo_audio_test">停止音频测试</string>
161161
<string name="echo_pretest">网络回路测试</string>
162162
<string name="lastmile_network_pretest">网络延迟测试</string>
163+
<string name="echo_record_countdown">录制倒计时</string>
164+
<string name="echo_playing_countdown">播放倒计时</string>
165+
<string name="recording_start">开始麦克风录制</string>
163166
</resources>

Android/APIExample/app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,7 @@
164164
<string name="stop_echo_audio_test">Stop Echo Audio Test</string>
165165
<string name="echo_pretest">Echo Test</string>
166166
<string name="lastmile_network_pretest">Lastmile Network Pretest</string>
167+
<string name="echo_record_countdown">Recording Countdown</string>
168+
<string name="echo_playing_countdown">Playing Countdown</string>
169+
<string name="recording_start">Recording on Microphone</string>
167170
</resources>

0 commit comments

Comments
 (0)