5454import io .agora .rtc2 .video .VideoCanvas ;
5555import io .agora .rtc2 .video .VideoEncoderConfiguration ;
5656
57- import java .lang .reflect .Field ;
58- import java .lang .reflect .Method ;
59-
6057/**
6158 * This example demonstrates how video can be flexibly switched between the camera stream and the
6259 * screen share stream during an audio-video call.
@@ -122,7 +119,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
122119 join .setOnClickListener (this );
123120
124121 screenPreview = view .findViewById (R .id .screen_preview );
125- // externalMediaPro = view.findViewById(R.id.media_projection_external);
122+ externalMediaPro = view .findViewById (R .id .media_projection_external );
126123 screenAudio = view .findViewById (R .id .screen_audio );
127124 screenAudioVolume = view .findViewById (R .id .screen_audio_volume );
128125 screenScenarioType = view .findViewById (R .id .spinner_screen_scenario_type );
@@ -218,6 +215,7 @@ private void enableNotifications() {
218215
219216 @ Override
220217 public void onDestroy () {
218+ stopMediaProjectionService ();
221219 /*leaveChannel and Destroy the RtcEngine instance*/
222220 if (engine != null ) {
223221 engine .leaveChannel ();
@@ -247,23 +245,8 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
247245 }
248246 screenCaptureParameters .captureAudio = checked ;
249247 engine .updateScreenCaptureParameters (screenCaptureParameters );
250- } /*else if (compoundButton == externalMediaPro) {
251- }*/
252- }
253-
254- @ Override
255- protected void onBackPressed () {
256- joined = false ;
257- stopMediaProjectionService ();
258- /*leaveChannel and Destroy the RtcEngine instance*/
259- if (engine != null ) {
260- engine .leaveChannel ();
261- engine .stopScreenCapture ();
262- engine .stopPreview ();
248+ } else if (compoundButton == externalMediaPro ) {
263249 }
264- handler .post (RtcEngine ::destroy );
265- engine = null ;
266- super .onBackPressed ();
267250 }
268251
269252 @ Override
@@ -331,7 +314,7 @@ public void onResume() {
331314 }
332315
333316 private void startMediaProjectionService () {
334- if (joined ) {
317+ // if (joined) {
335318 Context context = getContext ();
336319 if (context != null ) {
337320 Intent intent = new Intent (context , MediaProjectionService .class );
@@ -342,7 +325,7 @@ private void startMediaProjectionService() {
342325 }
343326 }
344327 }
345- }
328+ // }
346329
347330 private void stopMediaProjectionService () {
348331 Context context = getContext ();
@@ -353,6 +336,7 @@ private void stopMediaProjectionService() {
353336 }
354337
355338 private void requestScreenCapture () {
339+ startMediaProjectionService ();
356340 Intent intent = mediaProjectionManager .createScreenCaptureIntent ();
357341 mediaProjectionLauncher .launch (intent );
358342 }
@@ -576,6 +560,7 @@ public void onUserOffline(int uid, int reason) {
576560
577561 private void leaveChannel () {
578562 externalMediaPro .setEnabled (true );
563+ stopMediaProjectionService ();
579564 joined = false ;
580565 join .setText (getString (R .string .join ));
581566 fl_local .removeAllViews ();
0 commit comments