2121import io .agora .api .example .R ;
2222import io .agora .api .example .annotation .Example ;
2323import io .agora .api .example .common .BaseFragment ;
24+ import io .agora .api .example .utils .CommonUtil ;
2425import io .agora .rtc .Constants ;
2526import io .agora .rtc .IRtcEngineEventHandler ;
2627import io .agora .rtc .RtcEngine ;
3637import static io .agora .rtc .video .VideoEncoderConfiguration .STANDARD_BITRATE ;
3738import static io .agora .rtc .video .VideoEncoderConfiguration .VD_640x360 ;
3839
39- /**This example demonstrates how to customize the renderer to render the local scene of the remote video stream.*/
40+ /**
41+ * This example demonstrates how to customize the renderer to render the local scene of the remote video stream.
42+ */
4043@ Example (
4144 index = 8 ,
4245 group = ADVANCED ,
4346 name = R .string .item_customremoterender ,
4447 actionId = R .id .action_mainFragment_to_CustomRemoteRender ,
4548 tipsId = R .string .customremoterender
4649)
47- public class CustomRemoteVideoRender extends BaseFragment implements View .OnClickListener
48- {
50+ public class CustomRemoteVideoRender extends BaseFragment implements View .OnClickListener {
4951 private static final String TAG = CustomRemoteVideoRender .class .getSimpleName ();
5052
5153 private FrameLayout fl_local , fl_remote ;
@@ -57,15 +59,13 @@ public class CustomRemoteVideoRender extends BaseFragment implements View.OnClic
5759
5860 @ Nullable
5961 @ Override
60- public View onCreateView (@ NonNull LayoutInflater inflater , @ Nullable ViewGroup container , @ Nullable Bundle savedInstanceState )
61- {
62+ public View onCreateView (@ NonNull LayoutInflater inflater , @ Nullable ViewGroup container , @ Nullable Bundle savedInstanceState ) {
6263 View view = inflater .inflate (R .layout .fragment_custom_remoterender , container , false );
6364 return view ;
6465 }
6566
6667 @ Override
67- public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState )
68- {
68+ public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
6969 super .onViewCreated (view , savedInstanceState );
7070 join = view .findViewById (R .id .btn_join );
7171 et_channel = view .findViewById (R .id .et_channel );
@@ -75,17 +75,14 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7575 }
7676
7777 @ Override
78- public void onActivityCreated (@ Nullable Bundle savedInstanceState )
79- {
78+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
8079 super .onActivityCreated (savedInstanceState );
8180 // Check if the context is valid
8281 Context context = getContext ();
83- if (context == null )
84- {
82+ if (context == null ) {
8583 return ;
8684 }
87- try
88- {
85+ try {
8986 /**Creates an RtcEngine instance.
9087 * @param context The context of Android Activity
9188 * @param appId The App ID issued to you by Agora. See <a href="https://docs.agora.io/en/Agora%20Platform/token#get-an-app-id">
@@ -94,38 +91,32 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState)
9491 * The SDK uses this class to report to the app on SDK runtime events.*/
9592 engine = RtcEngine .create (context .getApplicationContext (), getString (R .string .agora_app_id ), iRtcEngineEventHandler );
9693 }
97- catch (Exception e )
98- {
94+ catch (Exception e ) {
9995 e .printStackTrace ();
10096 getActivity ().onBackPressed ();
10197 }
10298 }
10399
104100 @ Override
105- public void onDestroy ()
106- {
101+ public void onDestroy () {
107102 super .onDestroy ();
108103 /**leaveChannel and Destroy the RtcEngine instance*/
109- if (engine != null )
110- {
104+ if (engine != null ) {
111105 engine .leaveChannel ();
112106 }
113107 handler .post (RtcEngine ::destroy );
114108 engine = null ;
115109 }
116110
117111 @ Override
118- public void onClick (View v )
119- {
120- if (v .getId () == R .id .btn_join )
121- {
122- if (!joined )
123- {
112+ public void onClick (View v ) {
113+ if (v .getId () == R .id .btn_join ) {
114+ if (!joined ) {
115+ CommonUtil .hideInputBoard (getActivity (), et_channel );
124116 // call when join button hit
125117 String channelId = et_channel .getText ().toString ();
126118 // Check permission
127- if (AndPermission .hasPermissions (this , Permission .Group .STORAGE , Permission .Group .MICROPHONE , Permission .Group .CAMERA ))
128- {
119+ if (AndPermission .hasPermissions (this , Permission .Group .STORAGE , Permission .Group .MICROPHONE , Permission .Group .CAMERA )) {
129120 joinChannel (channelId );
130121 return ;
131122 }
@@ -139,9 +130,7 @@ public void onClick(View v)
139130 // Permissions Granted
140131 joinChannel (channelId );
141132 }).start ();
142- }
143- else
144- {
133+ } else {
145134 joined = false ;
146135 /**After joining a channel, the user must call the leaveChannel method to end the
147136 * call before joining another channel. This method returns 0 if the user leaves the
@@ -166,12 +155,10 @@ public void onClick(View v)
166155 }
167156 }
168157
169- private void joinChannel (String channelId )
170- {
158+ private void joinChannel (String channelId ) {
171159 // Check if the context is valid
172160 Context context = getContext ();
173- if (context == null )
174- {
161+ if (context == null ) {
175162 return ;
176163 }
177164
@@ -180,15 +167,15 @@ private void joinChannel(String channelId)
180167 // Local video is on the top
181168 surfaceView .setZOrderMediaOverlay (true );
182169 // Add to the local container
183- if (fl_local .getChildCount () > 0 )
184- {
170+ if (fl_local .getChildCount () > 0 ) {
185171 fl_local .removeAllViews ();
186172 }
187173 fl_local .addView (surfaceView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
188174 // Setup local video to render your local camera preview
189175 engine .setupLocalVideo (new VideoCanvas (surfaceView , RENDER_MODE_HIDDEN , 0 ));
190- // Set audio route to speaker
191- engine .setDefaultAudioRoutetoSpeakerphone (true );
176+ /**Set up to play remote sound with receiver*/
177+ engine .setDefaultAudioRoutetoSpeakerphone (false );
178+ engine .setEnableSpeakerphone (false );
192179
193180 /** Sets the channel profile of the Agora RtcEngine.
194181 CHANNEL_PROFILE_COMMUNICATION(0): (Default) The Communication profile.
@@ -215,15 +202,13 @@ private void joinChannel(String channelId)
215202 * A token generated at the server. This applies to scenarios with high-security requirements. For details, see
216203 * https://docs.agora.io/en/cloud-recording/token_server_java?platform=Java*/
217204 String accessToken = getString (R .string .agora_access_token );
218- if (TextUtils .equals (accessToken , "" ) || TextUtils .equals (accessToken , "<#YOUR ACCESS TOKEN#>" ))
219- {
205+ if (TextUtils .equals (accessToken , "" ) || TextUtils .equals (accessToken , "<#YOUR ACCESS TOKEN#>" )) {
220206 accessToken = null ;
221207 }
222208 /** Allows a user to join a channel.
223209 if you do not specify the uid, we will generate the uid for you*/
224210 int res = engine .joinChannel (accessToken , channelId , "Extra Optional Data" , 0 );
225- if (res != 0 )
226- {
211+ if (res != 0 ) {
227212 // Usually happens with invalid parameters
228213 // Error code description can be found at:
229214 // en: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html
@@ -239,21 +224,18 @@ private void joinChannel(String channelId)
239224 * IRtcEngineEventHandler is an abstract class providing default implementation.
240225 * The SDK uses this class to report to the app on SDK runtime events.
241226 */
242- private final IRtcEngineEventHandler iRtcEngineEventHandler = new IRtcEngineEventHandler ()
243- {
227+ private final IRtcEngineEventHandler iRtcEngineEventHandler = new IRtcEngineEventHandler () {
244228 /**Reports a warning during SDK runtime.
245229 * Warning code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_warn_code.html*/
246230 @ Override
247- public void onWarning (int warn )
248- {
231+ public void onWarning (int warn ) {
249232 Log .w (TAG , String .format ("onWarning code %d message %s" , warn , RtcEngine .getErrorDescription (warn )));
250233 }
251234
252235 /**Reports an error during SDK runtime.
253236 * Error code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html*/
254237 @ Override
255- public void onError (int err )
256- {
238+ public void onError (int err ) {
257239 Log .e (TAG , String .format ("onError code %d message %s" , err , RtcEngine .getErrorDescription (err )));
258240 showAlert (String .format ("onError code %d message %s" , err , RtcEngine .getErrorDescription (err )));
259241 }
@@ -262,8 +244,7 @@ public void onError(int err)
262244 * @param stats With this callback, the application retrieves the channel information,
263245 * such as the call duration and statistics.*/
264246 @ Override
265- public void onLeaveChannel (RtcStats stats )
266- {
247+ public void onLeaveChannel (RtcStats stats ) {
267248 super .onLeaveChannel (stats );
268249 Log .i (TAG , String .format ("local user %d leaveChannel!" , myUid ));
269250 showLongToast (String .format ("local user %d leaveChannel!" , myUid ));
@@ -276,17 +257,14 @@ public void onLeaveChannel(RtcStats stats)
276257 * @param uid User ID
277258 * @param elapsed Time elapsed (ms) from the user calling joinChannel until this callback is triggered*/
278259 @ Override
279- public void onJoinChannelSuccess (String channel , int uid , int elapsed )
280- {
260+ public void onJoinChannelSuccess (String channel , int uid , int elapsed ) {
281261 Log .i (TAG , String .format ("onJoinChannelSuccess channel %s uid %d" , channel , uid ));
282262 showLongToast (String .format ("onJoinChannelSuccess channel %s uid %d" , channel , uid ));
283263 myUid = uid ;
284264 joined = true ;
285- handler .post (new Runnable ()
286- {
265+ handler .post (new Runnable () {
287266 @ Override
288- public void run ()
289- {
267+ public void run () {
290268 join .setEnabled (true );
291269 join .setText (getString (R .string .leave ));
292270 }
@@ -326,8 +304,7 @@ public void run()
326304 * @param elapsed Time elapsed (ms) from the local user calling the joinChannel method
327305 * until the SDK triggers this callback.*/
328306 @ Override
329- public void onRemoteAudioStateChanged (int uid , int state , int reason , int elapsed )
330- {
307+ public void onRemoteAudioStateChanged (int uid , int state , int reason , int elapsed ) {
331308 super .onRemoteAudioStateChanged (uid , state , reason , elapsed );
332309 Log .i (TAG , "onRemoteAudioStateChanged->" + uid + ", state->" + state + ", reason->" + reason );
333310 }
@@ -370,8 +347,7 @@ public void onRemoteAudioStateChanged(int uid, int state, int reason, int elapse
370347 * @param elapsed Time elapsed (ms) from the local user calling the joinChannel method until
371348 * the SDK triggers this callback.*/
372349 @ Override
373- public void onRemoteVideoStateChanged (int uid , int state , int reason , int elapsed )
374- {
350+ public void onRemoteVideoStateChanged (int uid , int state , int reason , int elapsed ) {
375351 super .onRemoteVideoStateChanged (uid , state , reason , elapsed );
376352 Log .i (TAG , "onRemoteVideoStateChanged->" + uid + ", state->" + state + ", reason->" + reason );
377353 }
@@ -381,8 +357,7 @@ public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapse
381357 * @param elapsed Time delay (ms) from the local user calling joinChannel/setClientRole
382358 * until this callback is triggered.*/
383359 @ Override
384- public void onUserJoined (int uid , int elapsed )
385- {
360+ public void onUserJoined (int uid , int elapsed ) {
386361 super .onUserJoined (uid , elapsed );
387362 Log .i (TAG , "onUserJoined->" + uid );
388363 showLongToast (String .format ("user %d joined!" , uid ));
@@ -398,11 +373,10 @@ public void onUserJoined(int uid, int elapsed)
398373 * https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1mediaio_1_1_agora_surface_view.html#ab4dcf3124f4bb4b945a043143babe41b)*/
399374 AgoraSurfaceView surfaceView = new AgoraSurfaceView (getContext ());
400375 surfaceView .init (null );
401- surfaceView .setZOrderOnTop ( false );
376+ surfaceView .setZOrderMediaOverlay ( true );
402377 surfaceView .setBufferType (MediaIO .BufferType .BYTE_BUFFER );
403378 surfaceView .setPixelFormat (MediaIO .PixelFormat .I420 );
404- if (fl_remote .getChildCount () > 0 )
405- {
379+ if (fl_remote .getChildCount () > 0 ) {
406380 fl_remote .removeAllViews ();
407381 }
408382 fl_remote .addView (surfaceView );
@@ -426,8 +400,7 @@ public void onUserJoined(int uid, int elapsed)
426400 * USER_OFFLINE_BECOME_AUDIENCE(2): (Live broadcast only.) The client role switched from
427401 * the host to the audience.*/
428402 @ Override
429- public void onUserOffline (int uid , int reason )
430- {
403+ public void onUserOffline (int uid , int reason ) {
431404 Log .i (TAG , String .format ("user %d offline! reason:%d" , uid , reason ));
432405 showLongToast (String .format ("user %d offline! reason:%d" , uid , reason ));
433406 handler .post (new Runnable () {
0 commit comments