@@ -76,8 +76,7 @@ public class SwitchCameraScreenShare extends BaseFragment implements View.OnClic
7676 private static final String TAG = SwitchCameraScreenShare .class .getSimpleName ();
7777 private static final int PROJECTION_REQ_CODE = 1 << 2 ;
7878 private static final int DEFAULT_SHARE_FRAME_RATE = 15 ;
79- private FrameLayout fl_remote ;
80- private RelativeLayout fl_local ;
79+ private FrameLayout fl_camera , fl_screen ;
8180 private Button join , renderMode ;
8281 private Switch camera , screenShare ;
8382 private EditText et_channel ;
@@ -101,12 +100,11 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
101100 public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
102101 super .onViewCreated (view , savedInstanceState );
103102 join = view .findViewById (R .id .btn_join );
104- renderMode = view .findViewById (R .id .renderMode );
105103 camera = view .findViewById (R .id .camera );
106104 screenShare = view .findViewById (R .id .screenShare );
107105 et_channel = view .findViewById (R .id .et_channel );
108- fl_remote = view .findViewById (R .id .fl_remote );
109- fl_local = view .findViewById (R .id .fl_local );
106+ fl_camera = view .findViewById (R .id .fl_camera );
107+ fl_screen = view .findViewById (R .id .fl_screenshare );
110108 join .setOnClickListener (this );
111109 renderMode .setOnClickListener (this );
112110 camera .setOnCheckedChangeListener (this );
@@ -167,6 +165,7 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
167165 options .publishScreenTrack = true ;
168166 options .publishCameraTrack = false ;
169167 engine .updateChannelMediaOptions (options );
168+ addScreenSharePreview ();
170169 }
171170 }
172171
@@ -266,8 +265,9 @@ public void onClick(View v) {
266265 join .setText (getString (R .string .join ));
267266 camera .setEnabled (false );
268267 screenShare .setEnabled (false );
269- fl_remote .removeAllViews ();
270- fl_local .removeAllViews ();
268+ fl_camera .removeAllViews ();
269+ fl_screen .removeAllViews ();
270+ engine .stopPreview ();
271271 /**After joining a channel, the user must call the leaveChannel method to end the
272272 * call before joining another channel. This method returns 0 if the user leaves the
273273 * channel and releases all resources related to the call. This method call is
@@ -288,21 +288,10 @@ public void onClick(View v) {
288288 engine .leaveChannel ();
289289 TEXTUREVIEW = null ;
290290 }
291- } else if (v .getId () == R .id .renderMode ) {
292- if (remoteUid == -1 ) {
293- return ;
294- }
295- if (curRenderMode == RENDER_MODE_HIDDEN ) {
296- curRenderMode = RENDER_MODE_FIT ;
297- renderMode .setText (String .format (getString (R .string .rendermode ), getString (R .string .fit )));
298- } else if (curRenderMode == RENDER_MODE_FIT ) {
299- curRenderMode = RENDER_MODE_HIDDEN ;
300- renderMode .setText (String .format (getString (R .string .rendermode ), getString (R .string .hidden )));
301- }
302291 }
303292 }
304293
305- private void addLocalPreview () {
294+ private void addScreenSharePreview () {
306295 // Check if the context is valid
307296 Context context = getContext ();
308297 if (context == null ) {
@@ -311,26 +300,33 @@ private void addLocalPreview() {
311300
312301 // Create render view by RtcEngine
313302 SurfaceView surfaceView = new SurfaceView (context );
314- if (fl_local .getChildCount () > 0 ) {
315- fl_local .removeAllViews ();
303+ if (fl_screen .getChildCount () > 0 ) {
304+ fl_screen .removeAllViews ();
316305 }
317306 // Add to the local container
318- fl_local .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
307+ fl_screen .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
319308 // Setup local video to render your local camera preview
320- engine .setupLocalVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , 0 ));
309+ engine .setupLocalVideo (new VideoCanvas (surfaceView , Constants .RENDER_MODE_FIT , Constants .VIDEO_MIRROR_MODE_DISABLED , Constants .VIDEO_SOURCE_SCREEN_PRIMARY , 0 ));
310+ engine .startPreview ();
321311 }
322312
323- private void setRemotePreview (Context context ) {
324- /**Display remote video stream*/
325- SurfaceView remoteSurfaceView = new SurfaceView (context );
326- remoteSurfaceView .setZOrderMediaOverlay (true );
327- if (fl_remote .getChildCount () > 0 ) {
328- fl_remote .removeAllViews ();
313+ private void addCameraPreview () {
314+ // Check if the context is valid
315+ Context context = getContext ();
316+ if (context == null ) {
317+ return ;
318+ }
319+
320+ // Create render view by RtcEngine
321+ SurfaceView surfaceView = new SurfaceView (context );
322+ if (fl_camera .getChildCount () > 0 ) {
323+ fl_camera .removeAllViews ();
329324 }
330- fl_remote .addView (remoteSurfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT ,
331- ViewGroup .LayoutParams .MATCH_PARENT ));
332- /**Setup remote video to render*/
333- engine .setupRemoteVideo (new VideoCanvas (remoteSurfaceView , curRenderMode , remoteUid ));
325+ // Add to the local container
326+ fl_camera .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
327+ // Setup local video to render your local camera preview
328+ engine .setupLocalVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , 0 ));
329+ // engine.startPreview();
334330 }
335331
336332 private void joinChannel (String channelId ) {
@@ -382,9 +378,10 @@ private void joinChannel(String channelId) {
382378 }
383379 // Prevent repeated entry
384380 join .setEnabled (false );
381+ addCameraPreview ();
382+ engine .startPreview ();
385383 }
386384
387-
388385 /**
389386 * IRtcEngineEventHandler is an abstract class providing default implementation.
390387 * The SDK uses this class to report to the app on SDK runtime events.
@@ -429,7 +426,7 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
429426 public void onLocalVideoStateChanged (int localVideoState , int error ) {
430427 super .onLocalVideoStateChanged (localVideoState , error );
431428 if (localVideoState == 1 ) {
432- Log .e (TAG , "启动成功 " );
429+ Log .i (TAG , "local view published successfully! " );
433430 }
434431 }
435432
@@ -474,21 +471,6 @@ public void onLocalVideoStateChanged(int localVideoState, int error) {
474471 public void onRemoteVideoStateChanged (int uid , int state , int reason , int elapsed ) {
475472 super .onRemoteVideoStateChanged (uid , state , reason , elapsed );
476473 Log .i (TAG , "onRemoteVideoStateChanged:uid->" + uid + ", state->" + state );
477- if (state == REMOTE_VIDEO_STATE_STARTING ) {
478- /**Check if the context is correct*/
479- Context context = getContext ();
480- if (context == null ) {
481- return ;
482- }
483- handler .post (() ->
484- {
485- remoteUid = uid ;
486- renderMode .setEnabled (true );
487- renderMode .setText (String .format (getString (R .string .rendermode ), getString (R .string .hidden )));
488- curRenderMode = RENDER_MODE_HIDDEN ;
489- setRemotePreview (context );
490- });
491- }
492474 }
493475
494476 @ Override
@@ -522,13 +504,6 @@ public void onUserJoined(int uid, int elapsed) {
522504 public void onUserOffline (int uid , int reason ) {
523505 Log .i (TAG , String .format ("user %d offline! reason:%d" , uid , reason ));
524506 showLongToast (String .format ("user %d offline! reason:%d" , uid , reason ));
525- handler .post (() -> {
526- /**Clear render view
527- Note: The video will stay at its last frame, to completely remove it you will need to
528- remove the SurfaceView from its parent*/
529- engine .setupRemoteVideo (new VideoCanvas (null , RENDER_MODE_HIDDEN , uid ));
530- fl_remote .removeAllViews ();
531- });
532507 }
533508 };
534509
0 commit comments