SDK quickstart
Real-time audio engagement immerses people in the sounds of human connections, keeping them engaged in your app longer. Voice Calling enables you to add one-to-one or small-group voice communication with smooth, jitter-free streaming to your app. Agora's Voice SDK makes it easy to embed real-time audio chat into web, mobile and native apps. Thanks to Agora's intelligent and global Software Defined Real-time Network (SD-RTN™), you can rely on the highest available audio quality.
This page shows the minimum code you need to integrate high-quality, low-latency Voice Calling features into your app using Voice SDK.
Understand the tech
The following figure shows the workflow you need to implement to integrate Voice Calling feature into your app.
To start a voice call, implement the following steps in your app:
-
Set the role
To both publish and subscribe to audio streams, you set the client role as host. Set the role as audience, if you only need to play audio streams published by other users.
-
Join a channel
Call the method to join a channel; apps that pass the same channel name join the same channel. After joining a channel, hosts can publish audio streams to the channel and subscribe to each other.
-
Publish audio in the channel
Send audio stream to users in the channel.
-
Subscribe to audio in the channel
Receive and play audio streams from other users in the channel.
Prerequisites
In order to follow this procedure you must have:
- Android Studio 4.1 or higher.
- Android SDK API Level 24 or higher.
- A mobile device that runs Android 4.1 or higher.
-
A computer with Internet access.
Ensure that no firewall is blocking your network communication.
Project setup
To integrate Voice Calling into your app, do the following:
-
In Android Studio, create a new Phone and Tablet, Java 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 Voice SDK into your Android project. To do this:
-
In
/Gradle Scripts/build.gradle (Module: <project name>.app)
, add the following line underdependencies
: -
Replace
<artifact id>
and<version>
with appropriate values for the latest release. For example,io.agora.rtc:voice-sdk:4.0.0.4
. You can obtain the latest<artifact id>
and<version>
information using Maven Central Repository Search.
-
-
Add permissions for network and device access. In
/app/Manifests/AndroidManifest.xml
, add the following permissions after</application>
: -
To prevent obfuscating the code in Voice SDK, add the following line to
/Gradle Scripts/proguard-rules.pro
:
You are ready to add Voice Calling features to your app.
Implement a client for Voice Calling
When a user opens the app, you initialize Agora Engine. When the user taps a button, the app joins a channel. When another user joins the same channel, their audio is played in the app. This simple workflow enables you to concentrate on implementing Voice SDK features and not the UI bells and whistles.
This section shows how to use the Voice SDK to implement Voice Calling into your app, step-by-step.
Implement the user interface
In the interface, you add a Button
to join and leave a channel and a TextView
to display connection information. To create this interface, in /app/res/layout/activity_main.xml
, replace the contents of the file with the following:
You see an error in your IDE. This is because this layout refers to a method that you create later.
Handle the system logic
Import the necessary Android classes and handle the Android permissions.
-
Import the Android classes
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines afterpackage com.example.<project name>
: -
Handle the Android permissions
When your app launches, ensure that the permissions necessary to insert Voice Calling feature into the app are granted. If the permissions are not granted, use the built-in Android feature to request them; if they are granted, return
true
.In
/app/java/com.example.<projectname>/MainActivity
, add the following lines beforeonCreate
: -
Show status updates to your users
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines beforeonCreate
.
Implement the Voice Calling logic
The following figure shows the API call sequence of implementing Voice Calling.
To implement this logic, take the following steps:
-
Import the Voice SDK classes
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines after the lastimport
statement: -
Declare the variables that you use to create and join a channel
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines toMainActivity
class: -
Setup Agora Engine
To implement Voice Calling, you use Voice SDK to create an Agora Engine instance. In
/app/java/com.example.<projectname>/MainActivity
, add the following code beforeonCreate
. -
Handle and respond to Agora Engine events
To create an instance of
IRtcEngineEventHandler
, in theMainActivity
class, add the following lines aftersetupVoiceSDKEngine
: -
Configure the channel before joining
Before you join a channel, you use
ChannelMediaOptions
to set the client role, channel profile, and publishing and subscription options. ThejoinChannel
method securely connects the local user to a channel using the selectedoptions
and an authentication token. To do this, in theMainActivity
class, add the following method: -
Join and leave a channel
When the user presses the Join button, call
joinChannel
. When the user presses Leave, callleaveChannel
to exit the chat. In/app/java/com.example.<projectname>/MainActivity
, add the following code afterjoinChannel()
:
Start and stop your app
In this implementation, you initialize and destroy Agora Engine when the app opens and closes. The local user joins and leaves a channel using the same Agora Engine instance. In order to send the microphone audio stream to Agora, you need to ensure that the local user gives permission to access the microphone on the local device.
To elegantly start and stop your app:
-
Check that the app has the correct permissions and initiate Agora Engine
In
/app/java/com.example.<projectname>/MainActivity
, replaceonCreate
with the following code: -
Clean up the resources used by your app
When a user closes the app, use
onDestroy
to clean up all the resources you created. In/app/java/com.example.<projectname>/MainActivity
, addonDestroy
afteronCreate
:
Test your implementation
To ensure that you have implemented Voice Calling in your app:
-
Generate a temporary token in Agora Console.
-
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
In Android Studio, in
app/java/com.example.<projectname>/MainActivity
, updateappId
,channelName
andtoken
with the values for your temporary token. -
Connect a physical Android device to your development device.
-
In Android Studio, click Run app. A moment later you see the project installed on your device. If this is the first time you run the project, grant microphone access to your app.
-
Click Join to start a call. Now, you can talk to the remote user using your app.
Reference
This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.
-
Downloads shows you how to install Voice SDK manually.
-
To ensure communication security in a test or production environment, use a token server to generate token is recommended to ensure communication security, see Implement the authentication workflow.