Skip to content

Commit a859834

Browse files
committed
[BGF] Push to CDN, Play audio, Set Video Properties crash fixes, playAudioFile optimized.
1 parent dc892be commit a859834

5 files changed

Lines changed: 64 additions & 53 deletions

File tree

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

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
9090
mixingPlayoutVolBar = view.findViewById(R.id.mixingPlayoutVolBar);
9191
mixingVolBar = view.findViewById(R.id.mixingVolBar);
9292
mixingProgressBar = view.findViewById(R.id.mixingProgress);
93+
mixingProgressBar.setOnSeekBarChangeListener(this);
9394
mixingPlayoutVolBar.setOnSeekBarChangeListener(this);
9495
mixingPublishVolBar.setOnSeekBarChangeListener(this);
9596
mixingVolBar.setOnSeekBarChangeListener(this);
@@ -228,15 +229,10 @@ public void onClick(View v)
228229
else if(v.getId() == R.id.bgmStart)
229230
{
230231
engine.startAudioMixing(Constant.MIX_FILE_PATH, false, false, -1, 0);
231-
// String timeString = new SimpleDateFormat("mm:ss").format(engine.getAudioMixingDuration(Constant.MIX_FILE_PATH));
232-
String timeString = new SimpleDateFormat("mm:ss").format(engine.getAudioFileInfo(Constant.MIX_FILE_PATH));
233-
progressText.setText(timeString);
234232
startProgressTimer();
235233
}
236234
else if(v.getId() == R.id.bgmStop){
237235
engine.stopAudioMixing();
238-
progressText.setText("00:00");
239-
mixingProgressBar.setProgress(0);
240236
stopProgressTimer();
241237
}
242238
else if(v.getId() == R.id.bgmResume){
@@ -261,38 +257,17 @@ else if (v.getId() == R.id.btn_effect)
261257
}
262258

263259
private void stopProgressTimer() {
264-
if(!progressTimer.isCancelled()){
265-
progressTimer.cancel(true);
266-
}
260+
mixingProgressBar.setProgress(0);
261+
handler.removeCallbacksAndMessages(null);
267262
}
268263

269264
private void startProgressTimer() {
270-
if(!progressTimer.getStatus().equals(AsyncTask.Status.RUNNING)){
271-
progressTimer.execute();
272-
}
265+
int currentPosition = engine.getAudioMixingCurrentPosition();
266+
if(mixingProgressBar.getMax()!=0 && !mixingProgressBar.isPressed())
267+
mixingProgressBar.setProgress(currentPosition);
268+
handler.postDelayed(this::startProgressTimer, 1000);
273269
}
274270

275-
private final AsyncTask progressTimer = new AsyncTask() {
276-
@Override
277-
protected Object doInBackground(Object[] objects) {
278-
while(true){
279-
try {
280-
handler.post(new Runnable() {
281-
@Override
282-
public void run() {
283-
// final int result = (int) ((float) engine.getAudioMixingCurrentPosition() / (float) engine.getAudioMixingDuration(Constant.MIX_FILE_PATH) * 100);
284-
final int result = (int) ((float) engine.getAudioMixingCurrentPosition() / (float) engine.getAudioFileInfo(Constant.MIX_FILE_PATH) * 100);
285-
mixingProgressBar.setProgress(Long.valueOf(result).intValue());
286-
}
287-
});
288-
Thread.sleep(500);
289-
} catch (InterruptedException e) {
290-
Log.e(TAG, e.getMessage());
291-
}
292-
}
293-
}
294-
};
295-
296271
/**
297272
* @param channelId Specify the channel name that you want to join.
298273
* Users that input the same channel name join the same channel.*/
@@ -395,6 +370,7 @@ public void run()
395370
bgm_pause.setEnabled(true);
396371
bgm_stop.setEnabled(true);
397372
effect.setEnabled(true);
373+
engine.getAudioFileInfo(Constant.MIX_FILE_PATH);
398374
}
399375
});
400376
}
@@ -466,6 +442,12 @@ public void onUserOffline(int uid, int reason)
466442
Log.i(TAG, String.format("user %d offline! reason:%d", uid, reason));
467443
showLongToast(String.format("user %d offline! reason:%d", uid, reason));
468444
}
445+
446+
@Override
447+
public void onRequestAudioFileInfo(AudioFileInfo info, int error) {
448+
Log.d(TAG, "onRequestAudioFileInfo: "+info.filePath);
449+
handler.post(()-> mixingProgressBar.setMax(info.durationMs));
450+
}
469451
};
470452

471453
@Override
@@ -491,16 +473,26 @@ else if(seekBar.getId() == R.id.mixingVolBar){
491473
* @param volume: Audio mixing volume. The value ranges between 0 and 100 (default).
492474
*/
493475
engine.adjustAudioMixingVolume(progress);
476+
}else if(seekBar.getId() == R.id.mixingProgress){
477+
String durationText = io.agora.api.example.utils.TextUtils.durationFormat((long) progress);
478+
progressText.setText(durationText);
494479
}
480+
495481
}
496482

497483
@Override
498484
public void onStartTrackingTouch(SeekBar seekBar) {
499-
485+
// no need to update when user changes
486+
if(seekBar.getId() == R.id.mixingProgress)
487+
handler.removeCallbacksAndMessages(null);
500488
}
501489

502490
@Override
503491
public void onStopTrackingTouch(SeekBar seekBar) {
504-
492+
if(seekBar.getId() == R.id.mixingProgress) {
493+
engine.setAudioMixingPosition(seekBar.getProgress());
494+
handler.postDelayed(this::startProgressTimer, 1000);
495+
}
505496
}
497+
506498
}

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public void onDestroy() {
137137
if (engine != null) {
138138
engine.leaveChannel();
139139
}
140+
if(retryTask!=null) retryTask.cancel(true);
140141
handler.post(RtcEngine::destroy);
141142
engine = null;
142143
}
@@ -313,6 +314,22 @@ private void startPublish() {
313314
* This method adds only one stream HTTP/HTTPS URL address each time it is called.*/
314315
int code = engine.addPublishStreamUrl(et_url.getText().toString(), transCodeSwitch.isChecked());
315316
if(code == 0){
317+
retryTask = new AsyncTask() {
318+
@Override
319+
protected Object doInBackground(Object[] objects) {
320+
Integer result = null;
321+
for (int i = 0; i < MAX_RETRY_TIMES; i++) {
322+
try {
323+
Thread.sleep(60 * 1000);
324+
} catch (InterruptedException e) {
325+
Log.e(TAG, e.getMessage());
326+
break;
327+
}
328+
result = engine.addPublishStreamUrl(et_url.getText().toString(), transCodeSwitch.isChecked());
329+
}
330+
return result;
331+
}
332+
};
316333
retryTask.execute();
317334
}
318335
/**Prevent repeated entry*/
@@ -338,6 +355,7 @@ private void stopPublish() {
338355
* This method applies to Live Broadcast only.
339356
* This method removes only one stream RTMP URL address each time it is called.*/
340357
unpublishing = true;
358+
retryTask.cancel(true);
341359
int ret = engine.removePublishStreamUrl(et_url.getText().toString());
342360
}
343361

@@ -688,19 +706,5 @@ public void run() {
688706
}
689707
};
690708

691-
private final AsyncTask retryTask = new AsyncTask() {
692-
@Override
693-
protected Object doInBackground(Object[] objects) {
694-
Integer result = null;
695-
for (int i = 0; i < MAX_RETRY_TIMES; i++) {
696-
try {
697-
Thread.sleep(60 * 1000);
698-
} catch (InterruptedException e) {
699-
Log.e(TAG, e.getMessage());
700-
}
701-
result = engine.addPublishStreamUrl(et_url.getText().toString(), transCodeSwitch.isChecked());
702-
}
703-
return result;
704-
}
705-
};
709+
private AsyncTask retryTask;
706710
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static io.agora.rtc.video.VideoCanvas.RENDER_MODE_HIDDEN;
3939
import static io.agora.rtc.video.VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15;
4040
import static io.agora.rtc.video.VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE;
41+
import static io.agora.rtc.video.VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_LANDSCAPE;
4142
import static io.agora.rtc.video.VideoEncoderConfiguration.STANDARD_BITRATE;
4243
import static io.agora.rtc.video.VideoEncoderConfiguration.VD_640x360;
4344

@@ -277,9 +278,9 @@ private void joinChannel(String channelId)
277278

278279
engine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(
279280
value,
280-
VideoEncoderConfiguration.FRAME_RATE.valueOf(framerate.getSelectedItem().toString()),
281-
Integer.valueOf(et_bitrate.getText().toString()),
282-
VideoEncoderConfiguration.ORIENTATION_MODE.valueOf(orientation.getSelectedItem().toString())
281+
VideoEncoderConfiguration.FRAME_RATE.values()[framerate.getSelectedItemPosition()],
282+
Integer.parseInt(et_bitrate.getText().toString()),
283+
VideoEncoderConfiguration.ORIENTATION_MODE.values()[orientation.getSelectedItemPosition()]
283284
));
284285

285286
/**Please configure accessToken in the string_config file.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package io.agora.api.example.utils;
22

3+
import android.content.Context;
4+
5+
import java.util.Locale;
6+
import java.util.concurrent.TimeUnit;
7+
38
public class TextUtils {
49
/**
510
* Print thread stack
@@ -12,4 +17,13 @@ public static String formatStackTrace(StackTraceElement[] stackTrace) {
1217
}
1318
return sb.toString();
1419
}
20+
public static String durationFormat(Long duration) {
21+
long hours = TimeUnit.MILLISECONDS.toHours(duration)%60;
22+
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)%60;
23+
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) % 60;
24+
if(hours>0)
25+
return String.format(Locale.getDefault(),"%02d:%02d:%02d",hours, minutes, seconds);
26+
else
27+
return String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
28+
}
1529
}

Android/APIExample/app/src/main/res/layout/fragment_play_audio_files.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
android:id="@+id/mixingProgress"
3535
android:layout_width="0dp"
3636
android:layout_weight="1"
37-
android:layout_height="10dp"
37+
android:layout_height="wrap_content"
3838
android:layout_marginTop="5dp"
3939
android:layout_marginStart="16dp"
4040
android:layout_marginBottom="5dp"

0 commit comments

Comments
 (0)