Skip to content

Commit f5b335f

Browse files
committed
[Android]fix cdn streaming bugs(NMS-8446/NMS-8441).
1 parent 883ca0b commit f5b335f

7 files changed

Lines changed: 174 additions & 70 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ protected void onCreate(Bundle savedInstanceState) {
3030

3131
@Override
3232
public boolean onSupportNavigateUp() {
33-
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
34-
return NavigationUI.navigateUp(navController, appBarConfiguration)
35-
|| super.onSupportNavigateUp();
33+
getOnBackPressedDispatcher().onBackPressed();
34+
return true;
3635
}
3736

3837
@Override

Android/APIExample/app/src/main/java/io/agora/api/example/common/BaseFragment.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,43 @@
44
import android.os.Bundle;
55
import android.os.Handler;
66
import android.os.Looper;
7+
import android.view.View;
78
import android.widget.Toast;
89

10+
import androidx.activity.OnBackPressedCallback;
11+
import androidx.annotation.NonNull;
912
import androidx.annotation.Nullable;
1013
import androidx.appcompat.app.AlertDialog;
1114
import androidx.fragment.app.Fragment;
15+
import androidx.navigation.Navigation;
1216

1317
public class BaseFragment extends Fragment {
1418
protected Handler handler;
1519
private AlertDialog mAlertDialog;
20+
private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) {
21+
@Override
22+
public void handleOnBackPressed() {
23+
onBackPressed();
24+
}
25+
};
1626

1727
@Override
1828
public void onCreate(@Nullable Bundle savedInstanceState) {
1929
super.onCreate(savedInstanceState);
2030
handler = new Handler(Looper.getMainLooper());
31+
requireActivity().getOnBackPressedDispatcher().addCallback(onBackPressedCallback);
32+
}
33+
34+
@Override
35+
public void onAttach(@NonNull Context context) {
36+
super.onAttach(context);
37+
onBackPressedCallback.setEnabled(true);
38+
}
39+
40+
@Override
41+
public void onDetach() {
42+
super.onDetach();
43+
onBackPressedCallback.setEnabled(false);
2144
}
2245

2346
protected void showAlert(String message) {
@@ -47,6 +70,16 @@ protected final void showLongToast(final String msg) {
4770
});
4871
}
4972

73+
protected final void showShortToast(final String msg) {
74+
runOnUIThread(() -> {
75+
Context context = getContext();
76+
if (context == null) {
77+
return;
78+
}
79+
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
80+
});
81+
}
82+
5083
protected final void runOnUIThread(Runnable runnable) {
5184
this.runOnUIThread(runnable, 0);
5285
}
@@ -74,4 +107,11 @@ public void onDestroy() {
74107
mAlertDialog = null;
75108
}
76109
}
110+
111+
protected void onBackPressed() {
112+
View view = getView();
113+
if (view != null) {
114+
Navigation.findNavController(view).navigateUp();
115+
}
116+
}
77117
}

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

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import androidx.annotation.NonNull;
2424
import androidx.annotation.Nullable;
25+
import androidx.appcompat.app.AlertDialog;
2526

2627
import java.util.ArrayList;
2728
import java.util.List;
@@ -57,6 +58,8 @@ public class AudienceFragment extends BaseFragment implements IMediaPlayerObserv
5758
private SeekBar volSeekBar;
5859
private Switch rtcSwitcher;
5960
private Spinner channelSpinner;
61+
private AlertDialog mPlayerFailDialog;
62+
private AlertDialog mPlayerCompletedDialog;
6063

6164
@Nullable
6265
@Override
@@ -120,7 +123,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
120123
* The SDK uses this class to report to the app on SDK runtime events.
121124
*/
122125
config.mEventHandler = iRtcEngineEventHandler;
123-
config.mAreaCode = ((MainApplication)getActivity().getApplication()).getGlobalSettings().getAreaCode();
126+
config.mAreaCode = ((MainApplication) getActivity().getApplication()).getGlobalSettings().getAreaCode();
124127
engine = RtcEngine.create(config);
125128
/**
126129
* This parameter is for reporting the usages of APIExample to agora background.
@@ -161,22 +164,25 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
161164
engine.startPreview();
162165
// Set audio route to microPhone
163166
engine.setDefaultAudioRoutetoSpeakerphone(true);
164-
if(isAgoraChannel){
165-
mediaPlayer.openWithAgoraCDNSrc(getUrl(), 0);
166-
}
167-
else {
168-
mediaPlayer.open(getUrl(), 0);
169-
}
167+
openPlayerWithUrl();
170168
} catch (Exception e) {
171169
e.printStackTrace();
172170
getActivity().onBackPressed();
173171
}
174172
}
175173

174+
private void openPlayerWithUrl() {
175+
if (isAgoraChannel) {
176+
mediaPlayer.openWithAgoraCDNSrc(getUrl(), 0);
177+
} else {
178+
mediaPlayer.open(getUrl(), 0);
179+
}
180+
}
181+
176182
@Override
177183
public void onDestroy() {
178184
super.onDestroy();
179-
if (rtcStreaming){
185+
if (rtcStreaming) {
180186
engine.leaveChannel();
181187
}
182188
mediaPlayer.stop();
@@ -221,6 +227,7 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
221227
@Override
222228
public void run() {
223229
vol_control.setVisibility(View.VISIBLE);
230+
volSeekBar.setProgress(100);
224231
}
225232
});
226233
}
@@ -351,8 +358,7 @@ private void toggleVideoLayout(boolean isMultiple) {
351358
video_row2.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 0, 1));
352359
// Create render view by RtcEngine
353360
SurfaceView surfaceView = RtcEngine.CreateRendererView(getContext());
354-
if(fl_local.getChildCount() > 0)
355-
{
361+
if (fl_local.getChildCount() > 0) {
356362
fl_local.removeAllViews();
357363
}
358364
// Add to the local container
@@ -403,19 +409,54 @@ private String getUrl() {
403409

404410
@Override
405411
public void onPlayerStateChanged(io.agora.mediaplayer.Constants.MediaPlayerState mediaPlayerState, io.agora.mediaplayer.Constants.MediaPlayerError mediaPlayerError) {
406-
Log.i(TAG, "player state change to " + mediaPlayerState.name());
412+
showShortToast("player state change to " + mediaPlayerState.name());
407413
handler.post(new Runnable() {
408414
@Override
409415
public void run() {
410416
switch (mediaPlayerState) {
411417
case PLAYER_STATE_FAILED:
412-
showLongToast(String.format("media player error: %s", mediaPlayerError.name()));
418+
mediaPlayer.stop();
419+
//showLongToast(String.format("media player error: %s", mediaPlayerError.name()));
420+
if (mPlayerFailDialog == null) {
421+
mPlayerFailDialog = new AlertDialog.Builder(requireContext())
422+
.setTitle(R.string.tip)
423+
.setCancelable(false)
424+
.setNegativeButton(R.string.cancel, (dialog, which) -> {
425+
dialog.dismiss();
426+
onBackPressed();
427+
})
428+
.setPositiveButton(R.string.confirm, (dialog, which) -> openPlayerWithUrl())
429+
.create();
430+
}
431+
mPlayerFailDialog.setMessage(getString(R.string.media_player_error, mediaPlayerError.name()) + "\n\n" + getString(R.string.reopen_url_again));
432+
mPlayerFailDialog.show();
413433
break;
414434
case PLAYER_STATE_OPEN_COMPLETED:
415435
mediaPlayer.play();
416436
if (isAgoraChannel)
417437
loadAgoraChannels();
418438
rtcSwitcher.setEnabled(true);
439+
if (mPlayerFailDialog != null) {
440+
mPlayerFailDialog.dismiss();
441+
}
442+
break;
443+
case PLAYER_STATE_PLAYBACK_COMPLETED:
444+
mediaPlayer.stop();
445+
if (mPlayerCompletedDialog == null) {
446+
mPlayerCompletedDialog = new AlertDialog.Builder(requireContext())
447+
.setTitle(R.string.tip)
448+
.setMessage(getString(R.string.media_player_complete) + "\n\n" + getString(R.string.reopen_url_again))
449+
.setNegativeButton(R.string.cancel, (dialog, which) -> {
450+
dialog.dismiss();
451+
onBackPressed();
452+
})
453+
.setCancelable(false)
454+
.setPositiveButton(R.string.confirm, (dialog, which) -> {
455+
openPlayerWithUrl();
456+
})
457+
.create();
458+
}
459+
mPlayerCompletedDialog.show();
419460
break;
420461
case PLAYER_STATE_STOPPED:
421462
default:
@@ -434,7 +475,7 @@ private void loadAgoraChannels() {
434475
private List<String> getChannelArray(int count) {
435476
List<String> list = new ArrayList<>();
436477
for (int i = 0; i < count; i++) {
437-
list.add("Channel"+(i+1));
478+
list.add("Channel" + (i + 1));
438479
}
439480
return list;
440481
}
@@ -503,7 +544,7 @@ public void onAudioVolumeIndication(int i) {
503544
private final AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
504545
@Override
505546
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
506-
Log.i(TAG,"Start to switch cdn, current index is "+mediaPlayer.getAgoraCDNLineCount()+". target index is "+i);
547+
Log.i(TAG, "Start to switch cdn, current index is " + mediaPlayer.getAgoraCDNLineCount() + ". target index is " + i);
507548
mediaPlayer.switchAgoraCDNLineByIndex(i);
508549
}
509550

@@ -512,4 +553,14 @@ public void onNothingSelected(AdapterView<?> adapterView) {
512553

513554
}
514555
};
556+
557+
@Override
558+
protected void onBackPressed() {
559+
560+
if (rtcSwitcher.isChecked()) {
561+
rtcSwitcher.setChecked(false);
562+
} else {
563+
super.onBackPressed();
564+
}
565+
}
515566
}

0 commit comments

Comments
 (0)