SDK quickstart
Thanks to Agora’s intelligent and global Software Defined Real-time Network (Agora SD-RTN™), you can rely on the highest available video and audio quality.
This page shows the minimum code you need to integrate high-quality, low-latency Broadcast Streaming features into your app using Video SDK.
Understand the tech
This section explains how you can integrate Broadcast Streaming features into your app. The following figure shows the workflow you need to integrate this feature into your app.
To start a session, implement the following steps in your app:
-
Retrieve a token: A token is a computer-generated string that authenticates a user when your app joins a channel. In this guide you retrieve your token from Agora Console. To see how to create an authentication server for development purposes, see Implement the authentication workflow. To develop your own token generator and integrate it into your production IAM system, read Token generators.
-
Join a channel: Call methods to create and join a channel; apps that pass the same channel name join the same 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 Broadcast Streaming 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 Video SDK into your Android project:
These steps are for package install, if you prefer to manually install, follow the installation instructions.
- 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:full-sdk:4.0.1
.You can obtain the latest
<artifact id>
and<version>
information using Maven Central Repository Search.
- In
-
Add permissions for network and device access.
In
/app/Manifests/AndroidManifest.xml
, add the following permissions after</application>
: -
To prevent obfuscating the code in Video SDK, add the following line to
/Gradle Scripts/proguard-rules.pro
:
You are ready to add Broadcast Streaming features to your app.
Implement a client for Broadcast Streaming
When a user opens the app, you initialize Agora Engine. When the user taps a button, the app joins or leaves a channel. When another user joins the same channel, their video and audio is rendered in the app. This simple workflow enables you to concentrate on implementing Agora features and not UX bells and whistles.
This section shows how to use the Video SDK to implement Broadcast Streaming into your app, step-by-step.
Implement the user interface
In the interface, create frames for local and remote video, and the UI elements to join and leave a channel. In /app/res/layout/activity_main.xml
, replace the contents of the file with the following:
You see errors in your IDE. This is because this layout refers to methods 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 Android permissions
When your app launches, ensure that the permissions necessary to insert Video 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 channel logic
The following figure shows the API call sequence of implementing Broadcast Streaming.
To implement this logic, take the following steps:
-
Import the Video 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 Broadcast Streaming, you use Video 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
In
/app/java/com.example.<projectname>/MainActivity
, add the following lines aftersetupVideoSDKEngine
: -
Render video from the remote user in the channel
In Video SDK, the remote preview starts when a remote user subscribes to the channel. When a remote user joins a channel, you display the video stream in the interface. Use
mRtcEventHandler
to callsetupRemoteVideo
for remote video rendering. In/app/java/com.example.<projectname>/MainActivity
, add the following code afterSetupVideoSDKEngine
: -
Render video from the local user in the channel
To render local video, you create a
VideoCanvas
and set its uid to0
. In/app/java/com.example.<projectname>/MainActivity
, add the following lines aftersetupRemoteVideo
: -
Join the channel to start Broadcast Streaming
When the local user clicks the Join button, call
joinChannel
. This method securely connects the local user to a channel using the authentication token. In/app/java/com.example.<projectname>/MainActivity
, add the following code aftersetupLocalVideo
: -
Leave the channel when user ends the call
When your app is running, the user can leave or join a channel using the buttons available in the UI. When a user clicks Leave, use
leaveChannel
to exit the channel. In/app/java/com.example.<projectname>/MainActivity
, addleaveChannel
afterjoinChannel
:
Start and stop your app
In this implementation, you initiate 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 video and audio streams to Agora, you need to ensure that the local user gives permission to access the camera and 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 the user closes this app, use
onDestroy
to clean up all the resources you created. In/app/java/com.example.<projectname>/MainActivity
, addonDestroy
afteronCreate
:
Test your implementation
Agora recommends you run this project on a physical mobile device, as some simulators may not support the full features of this project. To ensure that you have implemented Broadcast Streaming 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, you need to grant microphone and camera access to 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 Video 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.
- For a more complete example, see the open source Broadcast Streaming example project on GitHub.