Start a Video Call
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.
Understand the tech
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:
- The App ID: A randomly generated string provided by Agora for identifying your app. You can get the App ID from Agora Console.
- The user ID: The unique identifier of a user. You need to specify the user ID yourself, and ensure that it is unique in the channel.
- A token: In a test or production environment, your app client retrieves tokens from a server in your security infrastructure. For this page, you use a temporary token with a validity period of 24 hours that you retrieve from Agora Console.
- The channel name: A string that identifies the channel for the video call.
Prerequisites
Before proceeding, ensure that your development environment meets the following requirements:
- Java Development Kit.
- Android Studio 3.0 or later.
- Android SDK API Level 16 or higher.
- A valid Agora account.
- An active Agora project with an App ID and a temporary token. For details, see Get Started with Agora.
- A computer with access to the internet. If your network has a firewall, follow the instructions in Firewall Requirements.
- A mobile device that runs Android 4.1 or later.
Project setup
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.
After creating the project, Android Studio automatically starts gradle sync. Ensure that the sync succeeds before you continue. -
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:The way to add the Maven Central dependency can be different if you set dependencyResolutionManagement in your Android project.b. In
/Gradle Scripts/build.gradle(Module: <projectname>.app)
, add the following lines to integrate the Agora Video SDK into your Android project: -
Add permissions for network and device access.
In
/app/Manifests/AndroidManifest.xml
, add the following permissions after</application>
: -
To prevent obfuscating the code in the Agora SDK, add the following line to
/Gradle Scripts/proguard-rules.pro
:
Implement a client for Video Call
This section shows how to use the Agora Video SDK to implement Video Call into your app step by step.
Create the UI
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:
Handle the Android system logic
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 afterpackage com.example.<projectname>
: -
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 theonCreate
function:
Implement the Video Call logic
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 afterimport android.os.Bundle;
: -
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 afterAppCompatActivity {
: -
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, theinitializeAndJoinChannel
function encapsulates these core methods.In
/app/java/com.example.<projectname>/MainActivity
, add the following lines after theonCreate
function: -
Add the remote interface when a remote user joins the channel.
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines after theinitializeAndJoinChannel
function:
Start and stop your app
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
, replaceonCreate
with the following code in theMainActivity
class. -
When the user closes this app, clean up all the resources you created in
initializeAndJoinChannel
.In
/app/java/com.example.<projectname>/MainActivity
, addonDestroy
after theonCreate
function.
Test your app
To test your app, follow the steps:
- Fill in the
appId
andtoken
parameters with the App ID and temporary token that you retrieve from Agora Console. FillchannelName
with the channel name that you use to generate the temporary token. - Connect an Android device to your computer, and click
Run 'app'
on your Android Studio. A moment later you will see the project installed on your device. - When the app launches, you should be able to see yourself on the local view.
- Ask a friend to join the video call with you on the demo app. Enter the same App ID and channel name. After your friend joins the channel, you should be able to see and hear each other.
Next steps
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.
See also
This section provides additional information for your reference.
Sample project
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.
Other approaches to integrate the SDK
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.