Start Interactive Live Audio Streaming
Use this guide to quickly start the interactive live audio streaming with the Agora Voice SDK for Android.
Understand the tech
The following figure shows the workflow to integrate into your app in order to add Interactive Live Streaming Premium functionality.
As shown in the figure, the workflow for adding Interactive Live Streaming Premium in your project is as follows:
-
Set the client role Each user in an Interactive Live Streaming Premium channel is either a host or an audience member. Hosts publish streams to the channel, and the audience subscribe to the streams.
-
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, app clients with the role of the host can publish audio and video. For an auidence memeber to send audio and video, you can call
setClientRole
to switch the client role.
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 live stream.
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.
Set up the development environment
-
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 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: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: -
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 the basic interactive live streaming
This section introduces how to use the Agora Voice SDK to start the interactive live audio streaming. The following figure shows the API call sequence of the interactive live audio streaming.
1. Create the UI
Create the user interface (UI) for the audio streaming in the layout file of your project. Skip to Import Classes if you already have a UI in your project.
You can also refer to the xml files under the layout path in the OpenLive-Voice-Only-Android demo project.
Example for creating the UI
2. Import Classes
Import the following classes in the activity file of your project:
3. Get the device permission
Call the checkSelfPermission
method to access the microphone of the Android device when launching the activity.
4. Initialize RtcEngine
Create and initialize the RtcEngine
object before calling any other Agora APIs.
In the string.xml
file, replace agora_app_id
with your App ID. Call the create
method and pass in the App ID to initialize the RtcEngine
object.
You can also listen for callback events, such as when the local user joins the channel. Do not implement UI operations in these callbacks.
5. Set the channel profile
After initializing the RtcEngine
object, call the setChannelProfile
method to set the channel profile as LIVE_BROADCASTING
.
One RtcEngine object uses one profile only. If you want to switch to another profile, release the current RtcEngine
object with the destroy
method and create a new one before calling the setChannelProfile
method.
6. Set the user role
A live-streaming channel has two user roles: BROADCASTER
and AUDIENCE
, and the default role is AUDIENCE
. After setting the channel profile to LIVE_BROADCASTING
, your app may use the following steps to set the client role:
- Allow the user to set the role as
BROADCASTER
orAUDIENCE
. - Call the
setClientRole
method and pass in the user role set by the user.
Note that in the live streaming, only the host can be heard. If you want to switch the user role after joining the channel, call the setClientRole
method.
7. Join a channel
After setting the user role, you can call the joinChannel
method to join a channel. In this method, set the following parameters:
token
: Pass a token that identifies the role and privilege of the user. You can set it as one of the following values:- A temporary token generated in Console. A temporary token is valid for 24 hours. For details, see Get a Temporary Token. When joining a channel, ensure that the channel name is the same with the one you use to generate the temporary token.
- A token generated at the server. This applies to scenarios with high-security requirements. For details, see Generate a token from Your Server. When joining a channel, ensure that the channel name and
uid
is the same with those you use to generate the token.
- If your project has enabled the app certificate, ensure that you provide a token.
- Ensure that you do not set
token
as "".
channelName
: Specify the channel name that you want to join.uid
: ID of the local user that is an integer and should be unique. If you setuid
as 0, the SDK assigns a user ID for the local user and returns it in theonJoinChannelSuccess
callback.Once the user joins the channel, the user subscribes to the audio streams of all the other users in the channel by default, giving rise to usage and billing calculation. If you do not want to subscribe to a specified stream or all remote streams, call themute
methods accordingly.
For more details on the parameter settings, see joinChannel
.
8. Leave the channel
Call the leaveChannel
method to leave the current channel according to your scenario, for example, when the streaming ends, when you need to close the app, or when your app runs in the background.
Sample code
You can find the complete code logic in the OpenLive-Voice-Only-Android demo project.
Run the project
Run the project on your Android device. When the audio streaming starts, all the audience can hear the host in the app.
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.
- Go to SDK Downloads, download the latest version of the Agora Voice 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/ |
include folder | /app/src/main/jniLibs/ |
x86 folder | /app/src/main/jniLibs/ |
x86_64 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.