Skip to content

Commit 40dc65e

Browse files
author
Xia Ning
committed
Support switching between foreground background view
1 parent 7e44f64 commit 40dc65e

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@
3131
import io.agora.rtc.video.VideoEncoderConfiguration;
3232

3333
import static io.agora.api.example.common.model.Examples.ADVANCED;
34-
import static io.agora.api.example.common.model.Examples.BASIC;
3534
import static io.agora.rtc.video.VideoCanvas.RENDER_MODE_HIDDEN;
36-
import static io.agora.rtc.video.VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15;
37-
import static io.agora.rtc.video.VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE;
3835
import static io.agora.rtc.video.VideoEncoderConfiguration.STANDARD_BITRATE;
39-
import static io.agora.rtc.video.VideoEncoderConfiguration.VD_640x360;
4036

4137
/**
4238
* This demo demonstrates how to make a one-to-one video call
@@ -51,14 +47,16 @@
5147
public class LiveStreaming extends BaseFragment implements View.OnClickListener {
5248
private static final String TAG = LiveStreaming.class.getSimpleName();
5349

54-
private FrameLayout fl_local, fl_remote;
50+
private FrameLayout foreGroundVideo, backGroundVideo;
5551
private Button join, publish, latency;
5652
private EditText et_channel;
5753
private RtcEngine engine;
5854
private int myUid;
55+
private int remoteUid;
5956
private boolean joined = false;
6057
private boolean isHost = false;
6158
private boolean isLowLatency = false;
59+
private boolean isLocalVideoForeground = false;
6260

6361
@Nullable
6462
@Override
@@ -79,8 +77,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7977
view.findViewById(R.id.btn_join).setOnClickListener(this);
8078
view.findViewById(R.id.btn_publish).setOnClickListener(this);
8179
view.findViewById(R.id.btn_latency).setOnClickListener(this);
82-
fl_local = view.findViewById(R.id.fl_local);
83-
fl_remote = view.findViewById(R.id.fl_remote);
80+
view.findViewById(R.id.foreground_video).setOnClickListener(this);
81+
foreGroundVideo = view.findViewById(R.id.background_video);
82+
backGroundVideo = view.findViewById(R.id.foreground_video);
8483
}
8584

8685
@Override
@@ -176,7 +175,43 @@ public void onClick(View v) {
176175
} else if (v.getId() == R.id.btn_latency) {
177176
isLowLatency = !isLowLatency;
178177
latency.setText(isLowLatency ? getString(R.string.disable_low_latency) : getString(R.string.enable_low_latency));
178+
} else if (v.getId() == R.id.foreground_video) {
179+
isLocalVideoForeground = !isLocalVideoForeground;
180+
if (foreGroundVideo.getChildCount() > 0) {
181+
foreGroundVideo.removeAllViews();
182+
}
183+
if (backGroundVideo.getChildCount() > 0) {
184+
backGroundVideo.removeAllViews();
185+
}
186+
// Create render view by RtcEngine
187+
SurfaceView localView = RtcEngine.CreateRendererView(getContext());
188+
SurfaceView remoteView = RtcEngine.CreateRendererView(getContext());
189+
if (isLocalVideoForeground){
190+
// Add to the local container
191+
foreGroundVideo.addView(localView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
192+
// Add to the remote container
193+
backGroundVideo.addView(remoteView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
194+
// Setup remote video to render
195+
engine.setupRemoteVideo(new VideoCanvas(remoteView, RENDER_MODE_HIDDEN, remoteUid));
196+
// Setup local video to render your local camera preview
197+
engine.setupLocalVideo(new VideoCanvas(localView, RENDER_MODE_HIDDEN, 0));
198+
remoteView.setZOrderMediaOverlay(true);
199+
remoteView.setZOrderOnTop(true);
200+
}
201+
else{
202+
// Add to the local container
203+
foreGroundVideo.addView(remoteView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
204+
// Add to the remote container
205+
backGroundVideo.addView(localView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
206+
// Setup local video to render your local camera preview
207+
engine.setupLocalVideo(new VideoCanvas(localView, RENDER_MODE_HIDDEN, 0));
208+
// Setup remote video to render
209+
engine.setupRemoteVideo(new VideoCanvas(remoteView, RENDER_MODE_HIDDEN, remoteUid));
210+
localView.setZOrderMediaOverlay(true);
211+
localView.setZOrderOnTop(true);
212+
}
179213
}
214+
180215
}
181216

182217
private void joinChannel(String channelId) {
@@ -188,11 +223,11 @@ private void joinChannel(String channelId) {
188223

189224
// Create render view by RtcEngine
190225
SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
191-
if (fl_local.getChildCount() > 0) {
192-
fl_local.removeAllViews();
226+
if (foreGroundVideo.getChildCount() > 0) {
227+
foreGroundVideo.removeAllViews();
193228
}
194229
// Add to the local container
195-
fl_local.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
230+
foreGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
196231
// Setup local video to render your local camera preview
197232
engine.setupLocalVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, 0));
198233
// Set audio route to microPhone
@@ -389,21 +424,27 @@ public void onUserJoined(int uid, int elapsed) {
389424
if (context == null) {
390425
return;
391426
}
427+
if(remoteUid != 0) {
428+
return;
429+
}
430+
else{
431+
remoteUid = uid;
432+
}
392433
handler.post(() ->
393434
{
394435
/**Display remote video stream*/
395436
SurfaceView surfaceView = null;
396-
if (fl_remote.getChildCount() > 0) {
397-
fl_remote.removeAllViews();
437+
if (backGroundVideo.getChildCount() > 0) {
438+
backGroundVideo.removeAllViews();
398439
}
399440
// Create render view by RtcEngine
400441
surfaceView = RtcEngine.CreateRendererView(context);
401442
surfaceView.setZOrderMediaOverlay(true);
402443
// Add to the remote container
403-
fl_remote.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
444+
backGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
404445

405446
// Setup remote video to render
406-
engine.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, uid));
447+
engine.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, remoteUid));
407448
});
408449
}
409450

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
tools:context=".examples.advanced.LiveStreaming">
88

99
<FrameLayout
10-
android:id="@+id/fl_local"
10+
android:id="@+id/background_video"
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent"
1313
android:layout_above="@+id/ll_join" >
1414
</FrameLayout>
1515

1616
<FrameLayout
17-
android:id="@+id/fl_remote"
17+
android:id="@+id/foreground_video"
1818
android:layout_width="150dp"
1919
android:layout_height="200dp"
2020
android:layout_alignParentTop="true"

0 commit comments

Comments
 (0)