Implementing Video Calling functionality in Agora for Android

I am going to tell you guys how to easily implement video calling function in Agora for Android. Just follow the steps below:

Setting up a New Project

  1. First, open Android Studio and click on Start a new Android Studio project.
  2. Now, click on Choose your project panel and choose Phone and Tablet>Empty Activity and click Next.
  3. Now click Finish.

Integrating the Agora SDK

Now we have to integrate the Agora SDK. For that we need to add the line given below in the /app/build.gradle file of your project.

dependencies {

// 2.9.2 is the latest version of the Agora SDK. You can set it to other versions.

implementation ‘io.agora.rtc:full-sdk:2.9.2’

implementation ‘io.agora.rtm:rtm-sdk:1.1.0’

implementation ‘com.google.firebase:firebase-analytics:17.2.0’

implementation ‘com.google.firebase:firebase-auth:19.1.0’

implementation ‘com.google.android.gms:play-services-auth:17.0.0’

}

apply plugin: ‘com.google.gms.google-services’

Now add the project permissions in the /app/src/main/AndroidManifest.xml file for device access according to you needs.

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”

package=”com.example.whatsappagora”>. <uses-permission android:name=”android.permission.READ_PHONE_STATE” />

<uses-permission android:name=”android.permission.INTERNET” />

<uses-permission android:name=”android.permission.RECORD_AUDIO” />

<uses-permission android:name=”android.permission.CAMERA” />

<uses-permission android:name=”android.permission.MODIFY_AUDIO_SETTINGS” />

<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

<uses-permission android:name=”android.permission.BLUETOOTH” />

<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

</manifest>

To prevent code obfuscation just add the line give below in the app/proguard-rules.pro file.

-keep class io.agora.**{*;}

Set up Sign-in

We need to allow users to log in to our app using their Google account like most of the social media applications. We can the following line to start Google authentication process:

Intent sianInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

startActivityForResult(sianInIntent, RC_SIGN_IN);

Also check out the following link to implement Google authentication: https://youtu.be/SXlidHy-Tb8

Implementing Video Call Function

We use the Agora Video SDK for implementing this functionality. Now, let us add the video logic to the application.

Channel Selection

Let’s implement onClickListener of call button that we have in the Channel Selection activity. When the user clicks the call button, if the user is in the group chat mode, we use the channel name one user inputed in the EditText. Or if the user is in peer-to-peer mode, the channel name will be sorted combination of use Google display. Now we pass channel name, user information and call mode to Video Call Activity.

Video Call Activity

Create the UI components in this activity: start_call/end_call, switch_camera and an audio_mute/audio_unmute button.

Now, we have to do the following things in onCreate() method in Video Call activity:

1. Initializing Agora Rtc Engine

For this, all you need to do is just call RtcEngine.create(context, appid, RtcEventHandler) to create RtmClient instance.

Now let’s implement different events on the RtcEngine using basic event handlers needed for this application.

private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {

2. Setting Up Local Video Canvas

You must call two functions to start a local video call: enableVideo() and setupLocalVideo() on RtcEngine instance.

In setupLocalVideo() function, a surfaceView is passed as parameter created by calling RtcEngine.CreateRendorView(context).

3. Joining a Channel

Next step is to join a channel by calling joinChannel() on the RtcEngine instance and pass the channel name.

Now, the RtcEngineEventHandler will trigger onJoinChannelSuccess() method. It will return the Agora Video ID in which we store the user informations.

Implementing Buttons onClick

We need to implement logic for three buttons in this activity.

1. Start/end a call

If we are in a call, when we click the start/end call button, we must finish the video call by calling leaveChannel(). Or else we will start a call by setting up the local video and joins the channel.

2. Switching the Camera

For using the switch camera button, we need to call switchCamera() on RtcEngine instance.

3. Audio mute/unmute

This button is implemented by calling the muteLocalAudioStream() on RtcEngine instance and then passing whether the user already muted.

Thats all. You have successfully created your own video calling app in android.