People engage longer when they see, hear, and interact with each other. The Agora SDK enables you to embed real-time voice and video interaction in any app, on any device, anywhere.
This page shows the minimum code you need to add video call into your app by using the Agora Video SDK for Android.
The following figure shows the workflow to integrate into your app in order to add Video Call functionality.
As shown in the figure, the workflow for adding Video Call in your project is as follows:
Retrieve a token
A token is the credential that authenticates a user when your app client joins a channel. In a test or production environment, your app client retrieves tokens from a server in your security infrastructure.
Join a channel
Call joinChannel
to create and join a channel. App clients that pass the same channel name join the same channel.
Publish and subscribe to audio and video in the channel
After joining a channel, the app client automatically publishes and subscribes to audio and video in the channel.
For an app client to join a channel, you need the following information:
Before proceeding, ensure that your development environment meets the following requirements:
Follow the steps to create the environment necessary to add video call into your app.
For new projects, in Android Studio, create a Phone and Tablet Android project with an Empty Activity.
Integrate the Video SDK into your project with Maven Central. For more integration methods, see Other approaches to integrate the SDK.
a. In /Gradle Scripts/build.gradle(Project: <projectname>)
, add the following lines to add the Maven Central dependency:
buildscript {
repositories {
...
mavenCentral()
}
...
}
allprojects {
repositories {
...
mavenCentral()
}
}
b. In /Gradle Scripts/build.gradle(Module: <projectname>.app)
, add the following lines to integrate the Agora Video SDK into your Android project:
...
dependencies {
...
// For x.y.z, fill in a specific SDK version number. For example, 3.5.0 or 3.7.0.2.
// Get the latest version number through the release notes.
implementation 'io.agora.rtc:full-sdk:x.y.z'
}
Add permissions for network and device access.
In /app/Manifests/AndroidManifest.xml
, add the following permissions after </application>
:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<!-- Add the following permission on devices running Android 12.0 or later -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
To prevent obfuscating the code in the Agora SDK, add the following line to /Gradle Scripts/proguard-rules.pro
:
-keep class io.agora.**{*;}
This section shows how to use the Agora Video SDK to implement Video Call into your app step by step.
In the interface, you have one frame for local video and another for remote video. In /app/res/layout/activity_main.xml
, replace the content with the following:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/local_video_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
<FrameLayout
android:id="@+id/remote_video_view_container"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/darker_gray" />
</RelativeLayout>
Take the following steps to import the necessary Android classes and handle the Android permissions.
Import Android classes
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after package com.example.<projectname>
:
// Java
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.view.SurfaceView;
import android.widget.FrameLayout;
// Kotlin
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.Manifest
import android.content.pm.PackageManager
import android.view.SurfaceView
import android.widget.FrameLayout
Handle the Android permissions
When your app launches, check if the permissions necessary to insert video calling functionality into the app are granted. If the permissions are not granted, use the built-in Android functionality to request them; if they are, return true
.
To implement the system logic, in /app/java/com.example.<projectname>/MainActivity
, add the following lines before the onCreate
function:
// Java
private static final int PERMISSION_REQ_ID = 22;
private static final String[] REQUESTED_PERMISSIONS = {
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA
};
private boolean checkSelfPermission(String permission, int requestCode) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
return false;
}
return true;
}
// Kotlin
private val PERMISSION_REQ_ID_RECORD_AUDIO = 22
private val PERMISSION_REQ_ID_CAMERA = PERMISSION_REQ_ID_RECORD_AUDIO + 1
private fun checkSelfPermission(permission: String, requestCode: Int): Boolean {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(permission),
requestCode)
return false
}
return true
}
When your app opens, you create an RtcEngine
instance, enable the video, join a channel, and publish the local video to the lower frame layout in the UI. When another user joins the channel, you app catches the join event and adds the remote video to the top frame layout in the UI.
The following figure shows the API call sequence of implementing Video Call.
To implement this logic, take the following steps:
Import the Agora classes.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after import android.os.Bundle;
:
// Java
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.video.VideoCanvas;
// Kotlin
import io.agora.rtc.IRtcEngineEventHandler
import io.agora.rtc.RtcEngine
import io.agora.rtc.video.VideoCanvas
Create the variables that you use to create and join a video call channel.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after AppCompatActivity {
:
// Java
// Fill the App ID of your project generated on Agora Console.
private String appId = "";
// Fill the channel name.
private String channelName = "";
// Fill the temp token generated on Agora Console.
private String token = "";
private RtcEngine mRtcEngine;
private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
@Override
// Listen for the remote user joining the channel to get the uid of the user.
public void onUserJoined(int uid, int elapsed) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Call setupRemoteVideo to set the remote video view after getting uid from the onUserJoined callback.
setupRemoteVideo(uid);
}
});
}
};
// Kotlin
// Fill the App ID of your project generated on Agora Console.
private val APP_ID = ""
// Fill the channel name.
private val CHANNEL = ""
// Fill the temp token generated on Agora Console.
private val TOKEN = ""
private var mRtcEngine: RtcEngine ?= null
private val mRtcEventHandler = object : IRtcEngineEventHandler() {
// Listen for the remote user joining the channel to get the uid of the user.
override fun onUserJoined(uid: Int, elapsed: Int) {
runOnUiThread {
// Call setupRemoteVideo to set the remote video view after getting uid from the onUserJoined callback.
setupRemoteVideo(uid)
}
}
}
Initialize the app and join the channel.
Call the following core methods to join a channel in the MainActivity
class. In the following sample code, the initializeAndJoinChannel
function encapsulates these core methods.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after the onCreate
function:
// Java
private void initializeAndJoinChannel() {
try {
mRtcEngine = RtcEngine.create(getBaseContext(), appId, mRtcEventHandler);
} catch (Exception e) {
throw new RuntimeException("Check the error.");
}
// By default, video is disabled, and you need to call enableVideo to start a video stream.
mRtcEngine.enableVideo();
FrameLayout container = findViewById(R.id.local_video_view_container);
// Call CreateRendererView to create a SurfaceView object and add it as a child to the FrameLayout.
SurfaceView surfaceView = RtcEngine.CreateRendererView(getBaseContext());
container.addView(surfaceView);
// Pass the SurfaceView object to Agora so that it renders the local video.
mRtcEngine.setupLocalVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, 0));
// Join the channel with a token.
mRtcEngine.joinChannel(token, channelName, "", 0);
}
// Kotlin
private fun initializeAndJoinChannel() {
try {
mRtcEngine = RtcEngine.create(baseContext, APP_ID, mRtcEventHandler)
} catch (e: Exception) {
}
// By default, video is disabled, and you need to call enableVideo to start a video stream.
mRtcEngine!!.enableVideo()
val localContainer = findViewById(R.id.local_video_view_container) as FrameLayout
// Call CreateRendererView to create a SurfaceView object and add it as a child to the FrameLayout.
val localFrame = RtcEngine.CreateRendererView(baseContext)
localContainer.addView(localFrame)
// Pass the SurfaceView object to Agora so that it renders the local video.
mRtcEngine!!.setupLocalVideo(VideoCanvas(localFrame, VideoCanvas.RENDER_MODE_FIT, 0))
// Join the channel with a token.
mRtcEngine!!.joinChannel(TOKEN, CHANNEL, "", 0)
}
Add the remote interface when a remote user joins the channel.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after the initializeAndJoinChannel
function:
// Java
private void setupRemoteVideo(int uid) {
FrameLayout container = findViewById(R.id.remote_video_view_container);
SurfaceView surfaceView = RtcEngine.CreateRendererView(getBaseContext());
surfaceView.setZOrderMediaOverlay(true);
container.addView(surfaceView);
mRtcEngine.setupRemoteVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, uid));
}
// Kotlin
private fun setupRemoteVideo(uid: Int) {
val remoteContainer = findViewById(R.id.remote_video_view_container) as FrameLayout
val remoteFrame = RtcEngine.CreateRendererView(baseContext)
remoteFrame.setZOrderMediaOverlay(true)
remoteContainer.addView(remoteFrame)
mRtcEngine!!.setupRemoteVideo(VideoCanvas(remoteFrame, VideoCanvas.RENDER_MODE_FIT, uid))
}
Now you have created the Video Call functionality, start and stop the app. In this implementation, a video call starts when the user opens your app. The call ends when the user closes your app.
To implement this function, do the following:
Check that the app has the correct permissions. If permissions are granted, call initializeAndJoinChannel
to join a video call channel.
In /app/java/com.example.<projectname>/MainActivity
, replace onCreate
with the following code in the MainActivity
class.
// Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// If all the permissions are granted, initialize the RtcEngine object and join a channel.
if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID) &&
checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID)) {
initializeAndJoinChannel();
}
}
// Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// If all the permissions are granted, initialize the RtcEngine object and join a channel.
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO) && checkSelfPermission(Manifest.permission.CAMERA, PERMISSION_REQ_ID_CAMERA)) {
initializeAndJoinChannel()
}
}
When the user closes this app, clean up all the resources you created in initializeAndJoinChannel
.
In /app/java/com.example.<projectname>/MainActivity
, add onDestroy
after the onCreate
function.
// Java
protected void onDestroy() {
super.onDestroy();
mRtcEngine.leaveChannel();
mRtcEngine.destroy();
}
// Kotlin
override fun onDestroy() {
super.onDestroy()
mRtcEngine?.leaveChannel()
RtcEngine.destroy()
}
To test your app, follow the steps:
appId
and token
parameters with the App ID and temporary token that you retrieve from Agora Console. Fill channelName
with the channel name that you use to generate the temporary token.Run 'app'
on your Android Studio. A moment later you will see the project installed on your device. Generating a token by hand is not helpful in a production context. Authenticate Your Users with Tokens shows you how to start video calling with a token that you retrieve from your server.
This section provides additional information for your reference.
Agora provides an open-source sample project Basic Video Call on GitHub that implements one-to-one video call and group video call for your reference.
In addition to integrating the Agora Video SDK for Android through MavenCentral, you can also import the SDK into your project by manually copying the SDK files.
Go to SDK Downloads, download the latest version of the Agora Video SDK, and extract the files from the downloaded SDK package.
Copy the following files or subfolders from the libs folder of the downloaded SDK package to the path of your project.
File or subfolder | Path of your project |
---|---|
agora-rtc-sdk.jar file |
/app/libs/ |
arm64-v8a folder |
/app/src/main/jniLibs/ |
armeabi-v7a folder |
/app/src/main/jniLibs/ |
x86 folder |
/app/src/main/jniLibs/ |
x86_64 folder |
/app/src/main/jniLibs/ |
include folder |
/app/src/main/jniLibs/ |
armeabi-v7a
folder to the armeabi
file of your project. Contact support@agora.io if you encounter any incompability issue.