11package io .agora .api .example .examples .advanced ;
22
3+ import static android .app .Activity .RESULT_OK ;
4+ import static io .agora .api .example .common .model .Examples .ADVANCED ;
5+ import static io .agora .rtc2 .video .VideoCanvas .RENDER_MODE_HIDDEN ;
6+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .FRAME_RATE .FRAME_RATE_FPS_15 ;
7+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .ORIENTATION_MODE .ORIENTATION_MODE_ADAPTIVE ;
8+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .STANDARD_BITRATE ;
9+ import static io .agora .rtc2 .video .VideoEncoderConfiguration .VD_640x360 ;
10+
311import android .app .Notification ;
412import android .app .NotificationChannel ;
513import android .app .NotificationManager ;
1725import android .util .Log ;
1826import android .view .LayoutInflater ;
1927import android .view .SurfaceView ;
20- import android .view .TextureView ;
2128import android .view .View ;
2229import android .view .ViewGroup ;
2330import android .widget .Button ;
2431import android .widget .CompoundButton ;
2532import android .widget .EditText ;
2633import android .widget .FrameLayout ;
27- import android .widget .RelativeLayout ;
2834import android .widget .Switch ;
2935
3036import androidx .annotation .NonNull ;
3541import com .yanzhenjie .permission .AndPermission ;
3642import com .yanzhenjie .permission .runtime .Permission ;
3743
44+ import java .util .Random ;
45+
3846import io .agora .api .example .R ;
3947import io .agora .api .example .annotation .Example ;
4048import io .agora .api .example .common .BaseFragment ;
5058import io .agora .rtc2 .video .VideoCanvas ;
5159import io .agora .rtc2 .video .VideoEncoderConfiguration ;
5260
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-
6661/**
6762 * This example demonstrates how video can be flexibly switched between the camera stream and the
6863 * screen share stream during an audio-video call.
@@ -80,7 +75,7 @@ public class SwitchCameraScreenShare extends BaseFragment implements View.OnClic
8075 private static final int DEFAULT_SHARE_FRAME_RATE = 15 ;
8176 private FrameLayout fl_camera , fl_screen ;
8277 private Button join ;
83- private Switch camera , screenShare ;
78+ private Switch camera , screenShare , screenSharePreview ;
8479 private EditText et_channel ;
8580 private int myUid , remoteUid = -1 ;
8681 private boolean joined = false ;
@@ -104,12 +99,14 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
10499 join = view .findViewById (R .id .btn_join );
105100 camera = view .findViewById (R .id .camera );
106101 screenShare = view .findViewById (R .id .screenShare );
102+ screenSharePreview = view .findViewById (R .id .screenSharePreview );
107103 et_channel = view .findViewById (R .id .et_channel );
108104 fl_camera = view .findViewById (R .id .fl_camera );
109105 fl_screen = view .findViewById (R .id .fl_screenshare );
110106 join .setOnClickListener (this );
111107 camera .setOnCheckedChangeListener (this );
112108 screenShare .setOnCheckedChangeListener (this );
109+ screenSharePreview .setOnCheckedChangeListener (this );
113110 }
114111
115112 @ Override
@@ -175,9 +172,11 @@ public void onDestroy() {
175172 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
176173 getActivity ().stopService (fgServiceIntent );
177174 }
178- TEXTUREVIEW = null ;
179175 /**leaveChannel and Destroy the RtcEngine instance*/
180176 if (engine != null ) {
177+ if (camera .isChecked ()){
178+ engine .leaveChannelEx (rtcConnection2 );
179+ }
181180 engine .leaveChannel ();
182181 engine .stopPreview ();
183182 }
@@ -206,11 +205,11 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
206205 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
207206 getActivity ().stopService (fgServiceIntent );
208207 }
209- }
210- handler .postDelayed (() -> {
211- options .publishScreenTrack = b ;
208+ options .publishScreenTrack = false ;
212209 engine .updateChannelMediaOptions (options );
213- }, 1000 );
210+ }
211+ screenSharePreview .setEnabled (b );
212+ screenSharePreview .setChecked (b );
214213 } else {
215214 showAlert (getString (R .string .lowversiontip ));
216215 }
@@ -233,7 +232,15 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
233232 }
234233 else {
235234 engine .leaveChannelEx (rtcConnection2 );
235+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_CAMERA_PRIMARY );
236236 }
237+ }else if (compoundButton .getId () == R .id .screenSharePreview ) {
238+ if (b ){
239+ addScreenSharePreview ();
240+ }else {
241+ fl_screen .removeAllViews ();
242+ engine .stopPreview (Constants .VideoSourceType .VIDEO_SOURCE_SCREEN_PRIMARY );
243+ }
237244 }
238245 }
239246
@@ -242,8 +249,6 @@ public void onClick(View v) {
242249 if (v .getId () == R .id .btn_join ) {
243250 if (!joined ) {
244251 CommonUtil .hideInputBoard (getActivity (), et_channel );
245- /**Instantiate the view ready to display the local preview screen*/
246- TEXTUREVIEW = new TextureView (getContext ());
247252 // call when join button hit
248253 String channelId = et_channel .getText ().toString ();
249254 // Check permission
@@ -266,9 +271,23 @@ public void onClick(View v) {
266271 join .setText (getString (R .string .join ));
267272 camera .setEnabled (false );
268273 screenShare .setEnabled (false );
274+ screenSharePreview .setEnabled (false );
269275 fl_camera .removeAllViews ();
270276 fl_screen .removeAllViews ();
271- engine .stopPreview ();
277+
278+ if (camera .isChecked ()) {
279+ engine .leaveChannelEx (rtcConnection2 );
280+ camera .setChecked (false );
281+ }
282+ if (screenSharePreview .isChecked ()){
283+ engine .stopPreview (Constants .VideoSourceType .VIDEO_SOURCE_SCREEN_PRIMARY );
284+ screenSharePreview .setChecked (false );
285+ }
286+ if (screenShare .isChecked ()){
287+ engine .stopScreenCapture ();
288+ screenShare .setChecked (false );
289+ }
290+ engine .stopPreview (Constants .VideoSourceType .VIDEO_SOURCE_CAMERA_PRIMARY );
272291 /**After joining a channel, the user must call the leaveChannel method to end the
273292 * call before joining another channel. This method returns 0 if the user leaves the
274293 * channel and releases all resources related to the call. This method call is
@@ -287,7 +306,8 @@ public void onClick(View v) {
287306 * 2:If you call the leaveChannel method during CDN live streaming, the SDK
288307 * triggers the removeInjectStreamUrl method.*/
289308 engine .leaveChannel ();
290- TEXTUREVIEW = null ;
309+
310+ //requireActivity().finish();
291311 }
292312 }
293313 }
@@ -307,8 +327,11 @@ private void addScreenSharePreview() {
307327 // Add to the local container
308328 fl_screen .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
309329 // 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 ();
330+ engine .setupLocalVideo (new VideoCanvas (surfaceView , Constants .RENDER_MODE_FIT ,
331+ Constants .VIDEO_MIRROR_MODE_DISABLED ,
332+ Constants .VIDEO_SOURCE_SCREEN_PRIMARY ,
333+ 0 ));
334+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_SCREEN_PRIMARY );
312335 }
313336
314337 private void addCameraPreview () {
@@ -326,27 +349,31 @@ private void addCameraPreview() {
326349 // Add to the local container
327350 fl_camera .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
328351 // Setup local video to render your local camera preview
329- engine .setupLocalVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , 0 ));
330- // engine.startPreview();
352+ engine .setupLocalVideo (new VideoCanvas (
353+ surfaceView ,
354+ RENDER_MODE_HIDDEN ,
355+ Constants .VIDEO_MIRROR_MODE_AUTO ,
356+ Constants .VIDEO_SOURCE_CAMERA_PRIMARY ,
357+ 0 ));
358+ engine .startPreview (Constants .VideoSourceType .VIDEO_SOURCE_CAMERA_PRIMARY );
331359 }
332360
333361 private void joinChannel (String channelId ) {
334362 engine .setParameters ("{\" che.video.mobile_1080p\" :true}" );
335363 engine .setClientRole (Constants .CLIENT_ROLE_BROADCASTER );
336364 // set options
365+ options = new ChannelMediaOptions ();
337366 options .clientRoleType = Constants .CLIENT_ROLE_BROADCASTER ;
338367 options .autoSubscribeVideo = true ;
339368 options .autoSubscribeAudio = true ;
340369 options .publishCameraTrack = false ;
341- options .publishScreenTrack = true ;
370+ options .publishScreenTrack = false ;
342371 options .publishAudioTrack = false ;
343372 options .enableAudioRecordingOrPlayout = false ;
344373 options .publishEncodedVideoTrack = false ;
345374
346375 /**Enable video module*/
347376 engine .enableVideo ();
348- // start preview
349- engine .startPreview ();
350377 // Setup video encoding configs
351378 engine .setVideoEncoderConfiguration (new VideoEncoderConfiguration (
352379 VD_640x360 ,
@@ -380,7 +407,6 @@ private void joinChannel(String channelId) {
380407 // Prevent repeated entry
381408 join .setEnabled (false );
382409 addCameraPreview ();
383- engine .startPreview ();
384410 }
385411
386412 /**
0 commit comments