Skip to main content

You are looking at Voice Calling v3.x Docs. The newest version is  Voice Calling 4.x

Start a Voice 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 voice call into your app by using the Agora Voice SDK for Android.

Understand the tech

The following figure shows the workflow of a voice call implemented by the Agora SDK.

1623991425414

As shown in the figure, the workflow for adding Voice Call in your project is as follows:

  1. 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.

  2. Join a channel Call joinChannel to create and join a channel. App clients that pass the same channel name join the same channel.

  3. Publish and subscribe to audio in the channel After joining a channel, the app client automatically publishes and subscribes to audio in the channel.

For an app client to join an Video SDK 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 voice 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 voice call into your app.

  1. 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.

  2. Integrate the Voice SDK into your project with Maven Central. For more integration methods, see Other approaches to intergrate the SDK.

    a. In /Gradle Scripts/build.gradle(Project: <projectname>), add the following lines to add the Maven Central dependency:


    _14
    buildscript {
    _14
    repositories {
    _14
    ...
    _14
    mavenCentral()
    _14
    }
    _14
    ...
    _14
    }
    _14
    _14
    allprojects {
    _14
    repositories {
    _14
    ...
    _14
    mavenCentral()
    _14
    }
    _14
    }

    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 Voice SDK into your Android project:


    _7
    ...
    _7
    dependencies {
    _7
    ...
    _7
    // For x.y.z, fill in a specific SDK version number. For example, 3.5.0 or 3.7.0.2.
    _7
    // Get the latest version number through the release notes.
    _7
    implementation 'io.agora.rtc:voice-sdk:x.y.z'
    _7
    }

  3. Add permissions for network and device access.

    In /app/Manifests/AndroidManifest.xml, add the following permissions after </application>:


    _8
    <uses-permission android:name="android.permission.INTERNET" />
    _8
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    _8
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    _8
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    _8
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    _8
    <uses-permission android:name="android.permission.BLUETOOTH" />
    _8
    <!-- Add the following permission on devices running Android 12.0 or later -->
    _8
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

  4. To prevent obfuscating the code in the Agora SDK, add the following line to /Gradle Scripts/proguard-rules.pro:


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

Implement a client for Voice Call

This section shows how to use the Agora Voice SDK to implement Voice Call into your app step by step.

Create the UI

In /app/res/layout/activity_main.xml, replace the content with the following:


_25
<?xml version="1.0" encoding="UTF-8"?>
_25
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
_25
xmlns:tools="http://schemas.android.com/tools"
_25
android:id="@+id/activity_main"
_25
android:layout_width="match_parent"
_25
android:layout_height="match_parent"
_25
tools:context=".MainActivity">
_25
_25
_25
<FrameLayout
_25
android:layout_width="match_parent"
_25
android:layout_height="match_parent"
_25
android:background="@android:color/darker_gray">
_25
_25
<TextView
_25
android:layout_width="match_parent"
_25
android:layout_height="wrap_content"
_25
android:layout_marginBottom="40dp"
_25
android:layout_marginLeft="16dp"
_25
android:layout_marginStart="16dp"
_25
android:layout_gravity="center_vertical|start"
_25
android:text="Welcome to the Agora Voice Call channel."/>
_25
_25
</FrameLayout>
_25
</RelativeLayout>

Handle the Android system logic

Refer to the following steps to import the necessary Android classes and handle the Android permissions.

  1. Import Android classes

    In /app/java/com.example.<projectname>/MainActivity, add the following lines after package com.example.<projectname>:


    _5
    // Java
    _5
    import androidx.core.app.ActivityCompat;
    _5
    import androidx.core.content.ContextCompat;
    _5
    import android.Manifest;
    _5
    import android.content.pm.PackageManager;


    _6
    // Kotlin
    _6
    import android.content.pm.PackageManager
    _6
    import androidx.core.app.ActivityCompat
    _6
    import androidx.core.content.ContextCompat
    _6
    import android.Manifest
    _6
    import java.lang.Exception

  2. 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.

    To implement the system logic, in /app/java/com.example.<projectname>/MainActivity, add the following lines before the onCreate function:


    _16
    // Java
    _16
    private static final int PERMISSION_REQ_ID = 22;
    _16
    _16
    private static final String[] REQUESTED_PERMISSIONS = {
    _16
    Manifest.permission.RECORD_AUDIO,
    _16
    Manifest.permission.CAMERA
    _16
    };
    _16
    _16
    private boolean checkSelfPermission(String permission, int requestCode) {
    _16
    if (ContextCompat.checkSelfPermission(this, permission) !=
    _16
    PackageManager.PERMISSION_GRANTED) {
    _16
    ActivityCompat.requestPermissions(this, REQUESTED_PERMISSIONS, requestCode);
    _16
    return false;
    _16
    }
    _16
    return true;
    _16
    }


    _14
    // Kotlin
    _14
    private val PERMISSION_REQ_ID_RECORD_AUDIO = 22
    _14
    private val PERMISSION_REQ_ID_CAMERA = PERMISSION_REQ_ID_RECORD_AUDIO + 1
    _14
    _14
    private fun checkSelfPermission(permission: String, requestCode: Int): Boolean {
    _14
    if (ContextCompat.checkSelfPermission(this, permission) !=
    _14
    PackageManager.PERMISSION_GRANTED) {
    _14
    ActivityCompat.requestPermissions(this,
    _14
    arrayOf(permission),
    _14
    requestCode)
    _14
    return false
    _14
    }
    _14
    return true
    _14
    }

Implement the Voice Call logic

When your app opens, you create an RtEngine instance, join a channel, and publish the local audio to the channel. When another user joins the channel, your apps catches the join event and receive the audio from the remote user.

The following figure shows the API call sequence of implementing Voice Call.

1630576315935

To implement this logic, take the following steps:

  1. Import the Agora classes.

    In /app/java/com.example.<projectname>/MainActivity, add the following lines after import android.os.Bundle;


    _3
    // Java
    _3
    import io.agora.rtc.RtcEngine;
    _3
    import io.agora.rtc.IRtcEngineEventHandler;


    _3
    // Kotlin
    _3
    import io.agora.rtc.RtcEngine
    _3
    import io.agora.rtc.IRtcEngineEventHandler

  2. Create the variables that you use to create and join a voice call channel.

    In /app/java/com.example.<projectname>/MainActivity, add the following lines after AppCompatActivity {:


    _10
    // Java
    _10
    // Fill the App ID of your project generated on Agora Console.
    _10
    private String appId = "";
    _10
    // Fill the channel name.
    _10
    private String channelName = "";
    _10
    // Fill the temp token generated on Agora Console.
    _10
    private String token = "";
    _10
    private RtcEngine mRtcEngine;
    _10
    private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
    _10
    };


    _10
    // Kotlin
    _10
    // Fill the App ID of your project generated on Agora Console.
    _10
    private val APP_ID = ""
    _10
    // Fill the channel name.
    _10
    private val CHANNEL = ""
    _10
    // Fill the temp token generated on Agora Console.
    _10
    private val TOKEN = ""
    _10
    private var mRtcEngine: RtcEngine ?= null
    _10
    private val mRtcEventHandler = object : IRtcEngineEventHandler() {
    _10
    }

  3. 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:


    _9
    // Java
    _9
    private void initializeAndJoinChannel() {
    _9
    try {
    _9
    mRtcEngine = RtcEngine.create(getBaseContext(), appId, mRtcEventHandler);
    _9
    } catch (Exception e) {
    _9
    throw new RuntimeException("Check the error");
    _9
    }
    _9
    mRtcEngine.joinChannel(token, channelName, "", 0);
    _9
    }


    _8
    // Kotlin
    _8
    private fun initializeAndJoinChannel() {
    _8
    try {
    _8
    mRtcEngine = RtcEngine.create(baseContext, APP_ID, mRtcEventHandler)
    _8
    } catch (e: Exception) {
    _8
    }
    _8
    mRtcEngine!!.joinChannel(TOKEN, CHANNEL, "", 0)
    _8
    }

Start and stop your app

Now you have created the Voice Call functionality, start and stop the app. In this implementation, a voice call starts when the user opens your app. The call ends when the user closes your app.

To implement this function, do the following:

  1. Check that the app has the corrent permissions. If permissions are granted, call initializeAndJoinChannel to join a voice call channel.

    In /app/java/com.example.<projectname>/MainActivity, replace onCreate with the following code in the MainActivity class.


    _9
    // Java
    _9
    @Override
    _9
    protected void onCreate(Bundle savedInstanceState) {
    _9
    super.onCreate(savedInstanceState);
    _9
    setContentView(R.layout.activity_main);
    _9
    if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID)) {
    _9
    initializeAndJoinChannel();
    _9
    }
    _9
    }


    _8
    // Kotlin
    _8
    override fun onCreate(savedInstanceState: Bundle?) {
    _8
    super.onCreate(savedInstanceState)
    _8
    setContentView(R.layout.activity_main)
    _8
    if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
    _8
    initializeAndJoinChannel();
    _8
    }
    _8
    }

  2. 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.


    _6
    // Java
    _6
    protected void onDestroy() {
    _6
    super.onDestroy();
    _6
    mRtcEngine.leaveChannel();
    _6
    mRtcEngine.destroy();
    _6
    }


    _6
    // Kotlin
    _6
    override fun onDestroy() {
    _6
    super.onDestroy()
    _6
    mRtcEngine?.leaveChannel()
    _6
    RtcEngine.destroy()
    _6
    }

Test your app

To test your app, follow the steps:

  1. Fill in the 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.
  2. 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.
  3. When the app lauches, you should be able to see the user interface we created.
  4. Ask a friend to join the voice 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 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

Other approaches to integrate the SDK

In addition to integrating the Agora Voice SDK for Android through MavenCentral, you can also import the SDK into your project by manually copying the SDK files.

  1. Go to SDK Downloads, download the latest version of the Agora Voice SDK, and extract the files from the downloaded SDK package.
  2. Copy the following files or subfolders from the libs folder of the downloaded SDK package to the path of your project.
File or subfolderPath of your project
agora-rtc-sdk.jar file/app/libs/
arm64-v8a folder/app/src/main/jniLibs/
armeabi-v7a folder/app/src/main/jniLibs/
include folder/app/src/main/jniLibs/
x86 folder/app/src/main/jniLibs/
x86_64 folder/app/src/main/jniLibs/
If you use the armeabi architecture, copy files from the armeabi-v7a folder to the armeabi file of your project. Contact support@agora.io if you encounter any incompability issue.

Voice Calling