11package io .agora .api .example .examples .advanced ;
22
3+ import static android .app .Activity .RESULT_OK ;
4+ import static io .agora .api .example .common .Constant .TEXTUREVIEW ;
5+ import static io .agora .api .example .common .model .Examples .ADVANCED ;
6+ import static io .agora .rtc2 .video .VideoCanvas .RENDER_MODE_HIDDEN ;
7+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .FRAME_RATE .FRAME_RATE_FPS_15 ;
8+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .ORIENTATION_MODE .ORIENTATION_MODE_ADAPTIVE ;
9+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .STANDARD_BITRATE ;
10+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .VD_640x360 ;
11+
312import android .app .Notification ;
413import android .app .NotificationChannel ;
514import android .app .NotificationManager ;
2433import android .widget .CompoundButton ;
2534import android .widget .EditText ;
2635import android .widget .FrameLayout ;
27- import android .widget .RelativeLayout ;
2836import android .widget .Switch ;
2937
3038import androidx .annotation .NonNull ;
3543import com .yanzhenjie .permission .AndPermission ;
3644import com .yanzhenjie .permission .runtime .Permission ;
3745
46+ import java .util .Random ;
47+
3848import io .agora .api .example .R ;
3949import io .agora .api .example .annotation .Example ;
4050import io .agora .api .example .common .BaseFragment ;
5060import io .agora .rtc2 .video .VideoCanvas ;
5161import io .agora .rtc2 .video .VideoEncoderConfiguration ;
5262
53- import java .util .Random ;
54-
55- import static android .app .Activity .RESULT_OK ;
56- import static io .agora .api .example .common .Constant .TEXTUREVIEW ;
57- import static io .agora .api .example .common .model .Examples .ADVANCED ;
58- import static io .agora .rtc2 .Constants .REMOTE_VIDEO_STATE_STARTING ;
59- import static io .agora .rtc2 .video .VideoCanvas .RENDER_MODE_FIT ;
60- import static io .agora .rtc2 .video .VideoCanvas .RENDER_MODE_HIDDEN ;
61- import static io .agora .rtc2 .video .VideoEncoderConfiguration .FRAME_RATE .FRAME_RATE_FPS_15 ;
62- import static io .agora .rtc2 .video .VideoEncoderConfiguration .ORIENTATION_MODE .ORIENTATION_MODE_ADAPTIVE ;
63- import static io .agora .rtc2 .video .VideoEncoderConfiguration .STANDARD_BITRATE ;
64- import static io .agora .rtc2 .video .VideoEncoderConfiguration .VD_640x360 ;
65-
6663/**
6764 * This example demonstrates how video can be flexibly switched between the camera stream and the
6865 * screen share stream during an audio-video call.
@@ -80,7 +77,7 @@ public class SwitchCameraScreenShare extends BaseFragment implements View.OnClic
8077 private static final int DEFAULT_SHARE_FRAME_RATE = 15 ;
8178 private FrameLayout fl_camera , fl_screen ;
8279 private Button join ;
83- private Switch camera , screenShare ;
80+ private Switch camera , screenShare , screenSharePreview ;
8481 private EditText et_channel ;
8582 private int myUid , remoteUid = -1 ;
8683 private boolean joined = false ;
@@ -104,12 +101,14 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
104101 join = view .findViewById (R .id .btn_join );
105102 camera = view .findViewById (R .id .camera );
106103 screenShare = view .findViewById (R .id .screenShare );
104+ screenSharePreview = view .findViewById (R .id .screenSharePreview );
107105 et_channel = view .findViewById (R .id .et_channel );
108106 fl_camera = view .findViewById (R .id .fl_camera );
109107 fl_screen = view .findViewById (R .id .fl_screenshare );
110108 join .setOnClickListener (this );
111109 camera .setOnCheckedChangeListener (this );
112110 screenShare .setOnCheckedChangeListener (this );
111+ screenSharePreview .setOnCheckedChangeListener (this );
113112 }
114113
115114 @ Override
@@ -178,6 +177,9 @@ public void onDestroy() {
178177 TEXTUREVIEW = null ;
179178 /**leaveChannel and Destroy the RtcEngine instance*/
180179 if (engine != null ) {
180+ if (camera .isChecked ()){
181+ engine .leaveChannelEx (rtcConnection2 );
182+ }
181183 engine .leaveChannel ();
182184 engine .stopPreview ();
183185 }
@@ -207,6 +209,8 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
207209 getActivity ().stopService (fgServiceIntent );
208210 }
209211 }
212+ screenSharePreview .setEnabled (b );
213+ screenSharePreview .setChecked (b );
210214 handler .postDelayed (() -> {
211215 options .publishScreenTrack = b ;
212216 engine .updateChannelMediaOptions (options );
@@ -233,7 +237,14 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
233237 }
234238 else {
235239 engine .leaveChannelEx (rtcConnection2 );
240+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_CAMERA_PRIMARY );
236241 }
242+ }else if (compoundButton .getId () == R .id .screenSharePreview ) {
243+ if (b ){
244+ addScreenSharePreview ();
245+ }else {
246+ engine .stopPreview (Constants .VideoSourceType .VIDEO_SOURCE_SCREEN_PRIMARY );
247+ }
237248 }
238249 }
239250
@@ -266,6 +277,7 @@ public void onClick(View v) {
266277 join .setText (getString (R .string .join ));
267278 camera .setEnabled (false );
268279 screenShare .setEnabled (false );
280+ screenSharePreview .setEnabled (false );
269281 fl_camera .removeAllViews ();
270282 fl_screen .removeAllViews ();
271283 engine .stopPreview ();
@@ -288,6 +300,8 @@ public void onClick(View v) {
288300 * triggers the removeInjectStreamUrl method.*/
289301 engine .leaveChannel ();
290302 TEXTUREVIEW = null ;
303+
304+ requireActivity ().finish ();
291305 }
292306 }
293307 }
@@ -307,8 +321,11 @@ private void addScreenSharePreview() {
307321 // Add to the local container
308322 fl_screen .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
309323 // Setup local video to render your local camera preview
310- engine .setupLocalVideo (new VideoCanvas (surfaceView , Constants .RENDER_MODE_FIT , Constants .VIDEO_MIRROR_MODE_DISABLED , Constants .VIDEO_SOURCE_SCREEN_PRIMARY , 0 ));
311- engine .startPreview ();
324+ engine .setupLocalVideo (new VideoCanvas (surfaceView , Constants .RENDER_MODE_FIT ,
325+ Constants .VIDEO_MIRROR_MODE_DISABLED ,
326+ Constants .VIDEO_SOURCE_SCREEN_PRIMARY ,
327+ 0 ));
328+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_SCREEN_PRIMARY );
312329 }
313330
314331 private void addCameraPreview () {
@@ -326,8 +343,13 @@ private void addCameraPreview() {
326343 // Add to the local container
327344 fl_camera .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
328345 // Setup local video to render your local camera preview
329- engine .setupLocalVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , 0 ));
330- // engine.startPreview();
346+ engine .setupLocalVideo (new VideoCanvas (
347+ surfaceView ,
348+ RENDER_MODE_HIDDEN ,
349+ Constants .VIDEO_MIRROR_MODE_AUTO ,
350+ Constants .VIDEO_SOURCE_CAMERA_PRIMARY ,
351+ 0 ));
352+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_CAMERA_PRIMARY );
331353 }
332354
333355 private void joinChannel (String channelId ) {
@@ -345,8 +367,6 @@ private void joinChannel(String channelId) {
345367
346368 /**Enable video module*/
347369 engine .enableVideo ();
348- // start preview
349- engine .startPreview ();
350370 // Setup video encoding configs
351371 engine .setVideoEncoderConfiguration (new VideoEncoderConfiguration (
352372 VD_640x360 ,
@@ -380,7 +400,6 @@ private void joinChannel(String channelId) {
380400 // Prevent repeated entry
381401 join .setEnabled (false );
382402 addCameraPreview ();
383- engine .startPreview ();
384403 }
385404
386405 /**
0 commit comments