Skip to content

Commit ec31c24

Browse files
author
zhangwei
committed
[Android]Fixs NMS-27353.
1 parent e85ba2f commit ec31c24

6 files changed

Lines changed: 142 additions & 50 deletions

File tree

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.agora.api.example.annotation.Example;
2525
import io.agora.api.example.common.BaseFragment;
2626
import io.agora.api.example.common.model.StatisticsInfo;
27+
import io.agora.api.example.utils.TokenUtils;
2728
import io.agora.rtc2.Constants;
2829
import io.agora.rtc2.EchoTestConfiguration;
2930
import io.agora.rtc2.IRtcEngineEventHandler;
@@ -160,36 +161,44 @@ public void onClick(View v) {
160161
btn_lastmile.setText("Testing ...");
161162
}
162163
else if (v.getId() == R.id.btn_echo){
163-
num = 0;
164-
engine.setClientRole(Constants.CLIENT_ROLE_BROADCASTER);
165-
EchoTestConfiguration config = new EchoTestConfiguration();
166-
config.enableVideo = false;
167-
config.enableAudio = true;
168-
config.intervalInSeconds = MAX_COUNT_DOWN;
169-
config.channelId = (new Random().nextInt(10000) + 100000) + "";
170-
engine.startEchoTest(config);
171-
btn_echo.setEnabled(false);
172-
btn_echo.setText("Recording on Microphone ...");
173-
echoTimer = new Timer(true);
174-
echoTimer.schedule(new TimerTask(){
175-
public void run() {
176-
num++;
177-
if(num >= MAX_COUNT_DOWN * 2){
178-
handler.post(() -> {
179-
btn_echo.setEnabled(true);
180-
btn_echo.setText(R.string.start);
181-
});
182-
engine.stopEchoTest();
183-
echoTimer.cancel();
184-
}
185-
else if(num >= MAX_COUNT_DOWN) {
186-
handler.post(() -> btn_echo.setText("PLaying with " + (MAX_COUNT_DOWN * 2 - num) + "Seconds"));
187-
}
188-
else{
189-
handler.post(() -> btn_echo.setText("Recording with " + (MAX_COUNT_DOWN - num) + "Seconds"));
190-
}
164+
String channelId = "AudioEchoTest" + (new Random().nextInt(1000) + 10000);
165+
TokenUtils.genToken(requireContext(), channelId, 0, ret -> {
166+
if (ret == null) {
167+
showAlert("Gen token error");
168+
return;
191169
}
192-
}, 1000, 1000);
170+
num = 0;
171+
engine.setClientRole(Constants.CLIENT_ROLE_BROADCASTER);
172+
EchoTestConfiguration config = new EchoTestConfiguration();
173+
config.enableVideo = false;
174+
config.enableAudio = true;
175+
config.intervalInSeconds = MAX_COUNT_DOWN;
176+
config.channelId = channelId;
177+
config.token = ret;
178+
engine.startEchoTest(config);
179+
btn_echo.setEnabled(false);
180+
btn_echo.setText("Recording on Microphone ...");
181+
echoTimer = new Timer(true);
182+
echoTimer.schedule(new TimerTask(){
183+
public void run() {
184+
num++;
185+
if(num >= MAX_COUNT_DOWN * 2){
186+
handler.post(() -> {
187+
btn_echo.setEnabled(true);
188+
btn_echo.setText(R.string.start);
189+
});
190+
engine.stopEchoTest();
191+
echoTimer.cancel();
192+
}
193+
else if(num >= MAX_COUNT_DOWN) {
194+
handler.post(() -> btn_echo.setText("PLaying with " + (MAX_COUNT_DOWN * 2 - num) + "Seconds"));
195+
}
196+
else{
197+
handler.post(() -> btn_echo.setText("Recording with " + (MAX_COUNT_DOWN - num) + "Seconds"));
198+
}
199+
}
200+
}, 1000, 1000);
201+
});
193202
}
194203
}
195204

Android/APIExample-Audio/app/src/main/java/io/agora/api/example/utils/TokenUtils.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import okhttp3.logging.HttpLoggingInterceptor;
2626

2727
public class TokenUtils {
28-
private final String TAG = "TokenGenerator";
28+
private static final String TAG = "TokenGenerator";
2929
private final static OkHttpClient client;
3030

3131
static {
@@ -36,6 +36,28 @@ public class TokenUtils {
3636
.build();
3737
}
3838

39+
public static void genToken(Context context, String channelName, int uid, OnTokenGenCallback<String> onGetToken) {
40+
String cert = context.getString(R.string.agora_app_certificate);
41+
if (cert.isEmpty()) {
42+
onGetToken.onTokenGen("");
43+
} else {
44+
gen(context.getString(R.string.agora_app_id), context.getString(R.string.agora_app_certificate), channelName, uid, ret -> {
45+
if (onGetToken != null) {
46+
runOnUiThread(() -> {
47+
onGetToken.onTokenGen(ret);
48+
});
49+
}
50+
}, ret -> {
51+
Log.e(TAG, "for requesting token error.", ret);
52+
if (onGetToken != null) {
53+
runOnUiThread(() -> {
54+
onGetToken.onTokenGen(null);
55+
});
56+
}
57+
});
58+
}
59+
}
60+
3961
public static void gen(Context context, String channelName, int uid, OnTokenGenCallback<String> onGetToken){
4062
gen(context.getString(R.string.agora_app_id), context.getString(R.string.agora_app_certificate), channelName, uid, ret -> {
4163
if(onGetToken != null){
@@ -44,7 +66,7 @@ public static void gen(Context context, String channelName, int uid, OnTokenGen
4466
});
4567
}
4668
}, ret -> {
47-
Log.e("TAG", "for requesting token error, use config token instead.");
69+
Log.e(TAG, "for requesting token error, use config token instead.");
4870
if (onGetToken != null) {
4971
runOnUiThread(() -> {
5072
onGetToken.onTokenGen(null);

Android/APIExample-Compose/app/src/main/java/io/agora/api/example/compose/samples/PreCallTest.kt

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import io.agora.api.example.compose.BuildConfig
3838
import io.agora.api.example.compose.R
3939
import io.agora.api.example.compose.data.SettingPreferences
4040
import io.agora.api.example.compose.ui.common.VideoCell
41+
import io.agora.api.example.compose.utils.TokenUtils
4142
import io.agora.rtc2.Constants
4243
import io.agora.rtc2.EchoTestConfiguration
4344
import io.agora.rtc2.IRtcEngineEventHandler
@@ -155,16 +156,24 @@ fun PreCallTest() {
155156
},
156157
onAudioEchoPretestClick = {
157158
isAudioEchoPretesting = true
158-
val config = EchoTestConfiguration()
159-
config.enableVideo = false
160-
config.enableAudio = true
161-
config.intervalInSeconds = ECHO_TEST_INTERVAL_IN_SECONDS
162-
config.channelId = "AudioEchoTest" + (Random().nextInt(1000) + 10000)
163-
rtcEngine.startEchoTest(config)
164-
handler.postDelayed({
165-
isAudioEchoPretesting = false
166-
rtcEngine.stopEchoTest()
167-
}, ECHO_TEST_INTERVAL_IN_SECONDS * 2 * 1000L)
159+
val channelId = "AudioEchoTest" + (Random().nextInt(1000) + 10000)
160+
TokenUtils.genToken(channelId, 0) { token ->
161+
if (token == null) {
162+
Toast.makeText(context, "Gen token error", Toast.LENGTH_LONG).show()
163+
return@genToken
164+
}
165+
val config = EchoTestConfiguration()
166+
config.enableVideo = false
167+
config.enableAudio = true
168+
config.intervalInSeconds = ECHO_TEST_INTERVAL_IN_SECONDS
169+
config.channelId = channelId
170+
config.token = token
171+
rtcEngine.startEchoTest(config)
172+
handler.postDelayed({
173+
isAudioEchoPretesting = false
174+
rtcEngine.stopEchoTest()
175+
}, ECHO_TEST_INTERVAL_IN_SECONDS * 2 * 1000L)
176+
}
168177
},
169178
onVideoEchoPretestClick = {
170179
isVideoEchoPretesting = true
@@ -174,13 +183,21 @@ fun PreCallTest() {
174183
}, ECHO_TEST_INTERVAL_IN_SECONDS * 2 * 1000L)
175184
},
176185
onVideoEchoViewCreated = {
177-
val config = EchoTestConfiguration()
178-
config.enableVideo = true
179-
config.view = it as? SurfaceView
180-
config.enableAudio = false
181-
config.intervalInSeconds = ECHO_TEST_INTERVAL_IN_SECONDS
182-
config.channelId = "VideoEchoTest" + (Random().nextInt(1000) + 10000)
183-
rtcEngine.startEchoTest(config)
186+
val channelId = "VideoEchoTest" + (Random().nextInt(1000) + 10000)
187+
TokenUtils.genToken(channelId, 0) { token ->
188+
if (token == null) {
189+
Toast.makeText(context, "Gen token error", Toast.LENGTH_LONG).show()
190+
return@genToken
191+
}
192+
val config = EchoTestConfiguration()
193+
config.enableVideo = true
194+
config.view = it as? SurfaceView
195+
config.enableAudio = false
196+
config.intervalInSeconds = ECHO_TEST_INTERVAL_IN_SECONDS
197+
config.channelId = channelId
198+
config.token = token
199+
rtcEngine.startEchoTest(config)
200+
}
184201
}
185202
)
186203
}

Android/APIExample-Compose/app/src/main/java/io/agora/api/example/compose/utils/TokenUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ private TokenUtils() {
4242
.build();
4343
}
4444

45+
public static void genToken(String channelName, int uid, OnTokenGenCallback<String> onGetToken) {
46+
String cert = BuildConfig.AGORA_APP_CERT;
47+
if (cert.isEmpty()) {
48+
onGetToken.onTokenGen("");
49+
} else {
50+
gen(BuildConfig.AGORA_APP_ID, BuildConfig.AGORA_APP_CERT, channelName, uid, ret -> {
51+
if (onGetToken != null) {
52+
runOnUiThread(() -> {
53+
onGetToken.onTokenGen(ret);
54+
});
55+
}
56+
}, ret -> {
57+
Log.e(TAG, "for requesting token error.", ret);
58+
if (onGetToken != null) {
59+
runOnUiThread(() -> {
60+
onGetToken.onTokenGen(null);
61+
});
62+
}
63+
});
64+
}
65+
}
66+
4567
/**
4668
* Gen.
4769
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void onClick(View v) {
162162
btn_lastmile.setText("Testing ...");
163163
} else if (v.getId() == R.id.btn_echo) {
164164
String channelId = "AudioEchoTest" + (new Random().nextInt(1000) + 10000);
165-
TokenUtils.gen(requireContext(), channelId, 0, ret -> {
165+
TokenUtils.genToken(requireContext(), channelId, 0, ret -> {
166166
if (ret == null) {
167167
showAlert("Gen token error");
168168
return;
@@ -200,7 +200,7 @@ public void run() {
200200
});
201201
} else if (v.getId() == R.id.btn_echo_video) {
202202
String channelId = "VideoEchoTest" + (new Random().nextInt(1000) + 10000);
203-
TokenUtils.gen(requireContext(), channelId, 0, ret -> {
203+
TokenUtils.genToken(requireContext(), channelId, 0, ret -> {
204204
if (ret == null) {
205205
showAlert("Gen token error");
206206
return;

Android/APIExample/app/src/main/java/io/agora/api/example/utils/TokenUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ private TokenUtils() {
4343
.build();
4444
}
4545

46+
public static void genToken(Context context, String channelName, int uid, OnTokenGenCallback<String> onGetToken) {
47+
String cert = context.getString(R.string.agora_app_certificate);
48+
if (cert.isEmpty()) {
49+
onGetToken.onTokenGen("");
50+
} else {
51+
gen(context.getString(R.string.agora_app_id), context.getString(R.string.agora_app_certificate), channelName, uid, ret -> {
52+
if (onGetToken != null) {
53+
runOnUiThread(() -> {
54+
onGetToken.onTokenGen(ret);
55+
});
56+
}
57+
}, ret -> {
58+
Log.e(TAG, "for requesting token error.", ret);
59+
if (onGetToken != null) {
60+
runOnUiThread(() -> {
61+
onGetToken.onTokenGen(null);
62+
});
63+
}
64+
});
65+
}
66+
}
67+
4668
/**
4769
* Gen.
4870
*

0 commit comments

Comments
 (0)