Real-time voice chatting immerses people in the sights and sounds of human connections, keeping them engaged in your app longer.
This page shows the minimum code you need to integrate high-quality, low-latency Voice Call function into your app using the SDK.
The following figure shows the workflow you need to integrate into your app in order to achieve Voice Call functionality.
To start voice call, implement the following steps in your app:
1. Set the role
Set both app clients as the host.
2. Join a channel
Call joinChannel
to create and join a channel. When using the same App ID, users who pass in the same channel name enter the same channel.
3 and 4. Publish and subscribe to audio in the channel
After joining a channel, both hosts can publish audio stream to the channel and subscribe to each other.
In order to create the environment necessary to integrate Voice Call into your app, do the following:
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.
/Gradle Scripts/build.gradle(Project: <projectname>)
, add the following lines to add the Maven Central dependency: buildscript {
repositories {
...
mavenCentral()
}
...
}
allprojects {
repositories {
...
mavenCentral()
}
}
In /Gradle Scripts/build.gradle(Module: <projectname>.app)
, add the following lines to integrate the Agora Video SDK into your Android project.
...
dependencies {
...
// Fill in a specific SDK version number (get the latest version from the SDK download). For example, 4.0.0 or 4.0.0.4
implementation 'io.agora.rtc:voice-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" />
To prevent obfuscating the code in the SDK, add the following line in /Gradle Scripts/proguard-rules.pro
file:
-keep class io.agora.**{*;}
This section shows how to use the Agora Video SDK to implement Voice Call into your app step by step.
In the interface, create 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_gravity="center_vertical|start"
android:text="Welcome to the Agora Voice Call channel."/>
</FrameLayout>
</RelativeLayout>
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>
:
import androidx.appcompat.app.AppCompatActivity;
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 Voice Call 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
.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines before the onCreate
function:
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;
}
The following figure and steps show the API call sequence of implementing Voice 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;
:
import io.agora.rtc2.Constants;
import io.agora.rtc2.IRtcEngineEventHandler;
import io.agora.rtc2.RtcEngine;
import io.agora.rtc2.RtcEngineConfig;
import io.agora.rtc2.ChannelMediaOptions;
Create the variables that you use to create and join channel.
In /app/java/com.example.<projectname>/MainActivity
, add the following lines after AppCompatActivity {
:
// 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 host joining the channel to get the uid of the host.
public void onUserJoined(int uid, int elapsed) {
};
Initialize the app and join the channel.
Call the core methods for joining a channel in the MainActivity
class, pass in your App ID, channel name, and temp token.
In the following reference code, the core methods are included in the initializeAndJoinChannel
function.
private void initializeAndJoinChannel() {
try {
RtcEngineConfig config = new RtcEngineConfig();
config.mContext = getBaseContext();
config.mAppId = appId;
config.mEventHandler = mRtcEventHandler;
mRtcEngine = RtcEngine.create(config);
} catch (Exception e) {
throw new RuntimeException("Check the error.");
}
ChannelMediaOptions options = new ChannelMediaOptions();
// Set both clients as the BROADCASTER.
options.clientRoleType = Constants.CLIENT_ROLE_BROADCASTER;
// Set the channel profile as BROADCASTING.
options.channelProfile = Constants.CHANNEL_PROFILE_LIVE_BROADCASTING;
// Join the channel with a temp token.
// You need to specify the user ID yourself, and ensure that it is unique in the channel.
mRtcEngine.joinChannel(token, channelName, 0, options);
}
Now you have created the Voice Call functionality. In this implementation, a video call starts when the user opens your app. The video call ends when the user closes your app.
Check that the app has the correct permissions. If permissions are granted, call initializeAndJoinChannel
to join channel.
In /app/java/com.example.<projectname>/MainActivity
, replace onCreate
with the following code in the MainActivity
class.
@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();
}
}
When the user closes this app, call leaveChannel
to release all resources related to the session. In /app/java/com.example.<projectname>/MainActivity
, add the following code in the MainActivity
class.
destroy
to clean up all the resources you created in initializeAndJoinChannel
. mRtcEngine.leaveChannel();
Please follow the test procedure as shown in the example.
Connect the Android devices to the computer.
Click Run 'app'
on your Android Studio. A moment later you can see the project installed on your device.
Grant microphone and camera access to your app.
When the app launches, ask a friend to use a second device to join the channel with the same App ID, token, and channel name.
You can hear each other.
In a test or production environment, use a token server to generate token is recommended to ensure communication security, see Authenticate Your Users with Tokens for details.
This section provides additional information for your reference.
In addition to integrating the Agora Voice SDK for Android through MavenCentral, you can also import the SDK into your project through JitPack or by manually copying the SDK files.
In /Gradle Scripts/build.gradle(Project: <projectname>)
, add JitPack to the list of repositories with the following lines:
all projects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
In /Gradle Scripts/build.gradle(Module: <projectname>.app)
, integrate the Agora Voice SDK into your Android project by adding the following lines:
...
dependencies {
...
// Fill in a specific SDK version number (get the latest version from the release notes). For example, 4.0.0-beta.1
implementation 'com.github.agorabuilder:agora-voice-beta:4.0.0-beta.1'
}
Go to SDK Downloads, download the latest version of the Agora Voice SDK, and extract the files from the downloaded SDK package.
Copy the necessary files and folders from the unzipped SDK package to your project, as shown in the following table.
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 in high_level_api |
/app/src/main/jniLibs/ |
Select Projet Files/app/libs/agora-rtc-sdk.jar
file on navigation bar, right click and then select add as a library
.
Agora provides an open source interactive live broadcast example project JoinChannelAudio on GitHub for your reference.