Agora.io
Published in

Agora.io

1-to-1 Video Chat App on Android using Agora.io

In this post, we’ll build a basic video chat app in 10 easy steps, using the Agora Video SDK for Android.

Prerequisites

Step 1: Getting your App ID

Once you finish the sign-up process, you are redirected to the Agora Console. Open the Projects tab on the left-hand nav to see your default project’s App ID.d

JCenter Integration

Add the following line in your project level /app/build.gradle:

allprojects {
repositories { ...
// Jit pack
maven { url ‘https://www.jitpack.io' }
}
}

Add the following line in the /app/build.gradle file of your project:

dependencies {
...
//Agora RTC SDK for video call
implementation 'com.github.agorabuilder:native-full-sdk:3.4.1'
}

Manually Integration

In the final project I use JCenter to make updating easier, but it’s always nice to demonstrate how to add the SDK manually. First, download the Agora Video SDK for Android from Agora.io Developer Center.

In your empty Android project, add the SDK libs folder into your Android project’s libs folder (from your file explorer):

I used the empty libs folder generated by Android Studio

Make sure to note the location of the libs folder in relation to the build.gradle. Next, it’s time to set the storage directory of the libs folder in the app/src/main/build.gradle dependencies list. Since Gradle reads paths relative to the build.gradle file we can add this fileTree declaration to let Gradle know to inlcude the libs folder.

make sure the PATH to libs matches the location of the libs folder relative to the build.gradle (app)

Next, make sure to add the sourceSets within the build.gradle, again make sure the PATH to the libs folder is relative to the build.gradle file.

note: the PATH will be the same as the one declared within the dependancies block

Set up Agora App ID

Next, its time to add your Agora.io App ID (see Step-1) to the Android project’s Strings.xml (app/src/main/res/values/Strings.xml).

<resources>
<string name="app_name">Agora-Android-Video-Tutorial</string>
<string name="agora_app_id"><#YOUR APP ID#></string>
</resources>

The next step is to add the appropriate permissions within Manifest.xml

The final step is to prevent obfuscation of the Agora classes. This might sound complex, but it’s really very simple. In the proguard-rules.pro file, add a
-keep class configuration for the Agora SDK.

-keep class io.agora.**{*;}
This prevents obfuscation of the Agora SDK public class names.

Step 4: Setting Up Views

Now that we have the Agora.io SDK integrated, let’s set up our UI. I will breeze through this portion since we are using standard UI elements.

I chose to use ImageViews instead of Buttons but you could use either.

In the example, I chose to use ImageView instead of Button for the various UI elements. Either works. The important part is to note that there are functions that we link to using the onClick property.

Step 5: Checking Permissions

I know what you must be thinking: “Didn’t we already set up the permissions?” Earlier, we let the application’s Manifest know which permissions our app plans to use, but we still have to explicitly request the user to grant these permissions. Don’t worry — this is the final step in getting the boilerplate project running, and it’s painless.

First, let’s declare which permissions we want to request:

// Permissions
private static final int PERMISSION_REQ_ID = 22;
private static final String[] REQUESTED_PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};

Next, we set up a couple of functions to help us.

First, we have a method that requests permissions for a given permission string and code.

Next, we have a callback method that gets called after the user has responded to the permissions request prompt.

Last, in our class’s onCreate we check if our permissions were granted. If they were not, the above methods handle the requests

Step 6: Initializing the Agora.io SDK

Now that we have our view, we are ready to initialize the Agora.io SDK, set up the user profile, and set the video quality settings.

In the previous step, you may have noticed a couple places that I call initAgoraEngine(). Before we can dive into the initialization, we need to make sure that our activity has access to an instance of the Agora.io RtcEngine.

In our MainActivity class, we need to declare a class property to store our instance of RtcEngine.

private RtcEngine mRtcEngine;

Now it’s time to initialize. After all the boilerplate setup, we are finally at the step where we can start playing with the Agora.io engine!

Go ahead and declare your initAgoraEngine() method in your class. In this function, we will create a new instance of the RtcEngine using the baseContext, the Agora App ID (declared above), and an instance of the RtcEngineEventHandler (we’ll get into this a little later).

Once we have our new instance, it’s time to set up our user’s session. Here we can set the Channel Profile to Communication, since this is a video chat and not a broadcast . This is also where we configure our video encoder settings.

Step 7: Connecting the Video Streams

Before we can join a call we need to be able to present the local video stream to the user via the UI elements we set up earlier (step 4). For reference, below is the UI element’s id that our local video will render into:

android:id="@+id/floating_video_container"

In the first line, we get a reference for the UI element that will act as our parent view for our video stream. The second step is to use the RtcEngine to create a SurfaceView that renders the stream from the front camera. We also set the new videoSurface to render on top of its parent view. The next step is to add the videoSurface as a subview of the UI element. Last, we pass the videoSurface to the engine as part of a VideoCanvas object. We leave the uid parameter blank so that the SDK can handle creating a dynamic ID for each user.

Now, that we have our local video feed set up, we need to use a similar function to connect our remote video stream.

The main difference between the remote video and the local video is the user ID parameter that gets passed to the engine as part of the VideoCanvas object. The last line sets the fallback option: If the video degrades, the engine reverts to audio only:

Step 8: Setting Up the SDK Event Handler

Earlier, I made a reference to the RtcEngineEventHandler, and now it’s time to declare it as a property of our MainActivity class. The engine will call these methods from the RtcEngineEventHandler.

Each event triggers some fairly straightforward functions, including one we wrote in the previous step. In the interest of brevity, I provide the code below, but I don’t give an in-depth breakdown.

feel free to leave a comment if anything is unclear

Step 9: Joining and Leaving Channels

I know what you’re thinking, step 9 ?! Don’t sweat it — the next two steps are very simple. Let’s start by joining a call.

Note: if you do not specify the uid when joining the channel, the engine will assign one

As you can see from the first line, Agora SDK makes it simple: The engine calls joinChannel, passing in the channel name followed by the call to set up our local video stream (Step 7).

note: the remove video method is the same used in step 8

Leaving the channel is even simpler: The engine calls leaveChannel. Above, you’ll notice there are a few lines to remove the video stream subviews from each UI element.

Step 10: Adding Other Functionality

The last remaining parts are related to connecting the UI elements for toggling the microphone and video stream on the local device. Let’s start with the audio toggle.

First, we get the reference to our button and then check if it has been toggled on or off using isSelected(). Once we have updated the UI element state, we pass the button’s updated state to the RtcEngine.

Moving on to the video toggle:

As with the audio toggle, we check or update the button’s state using isSelected() and then pass that to the RtcEngine. To give a better visual representation of the video being muted, we hide or show the videoSurface.

I hope you enjoyed reading along and working together on creating a 1-to-1 video vhat Android app using the Agora.io SDK.

Full source code:

If you have any questions or comments, invite you to join the Agora Developer Slack community.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hermes

CTO @ Arlene.io … former Director of DevRel @Agora.io & AR Solutions Engineer @ Blippar — If you can close your eyes & picture it, I can find a way to build it