Building your own RTE software can be a little troublesome: maintaining a server and load balancing while also providing low latency can make even the best of us go nuts.
So how can you add RTE to your Android application without losing your mind and while saving time? Luckily, we have the Agora RTE SDK available to provide support across all platforms. In this tutorial, I will walk you through the process of subscribing to multiple channels using the Agora Android SDK.
- An Agora developer account (see How to get started)
- Android Studio
- A basic understanding of Android development
Why would someone join multiple channels?
Before we jump into the development process, let’s look at why anyone needs to subscribe to multiple channels.
The main reason for joining multiple channels is that you can track the activities of multiple groups. Among the various use cases are breakout rooms, multiple sessions, waiting rooms, and active sessions.
- Open Android Studio, create a project, and select the template as an Empty Activity.
- Give a suitable name to your application and click Finish.
- Add the necessary dependencies to your build.gradle (app level) file.
- Add the relevant user permissions to AndroidManifest.xml.
All done! This will create a sample Android project with the name that you provided during the initial steps. Now, we are going to build our application on top of this.
Building our Login Page
The idea behind building a login page is that it reads two channels that the user wants to join. For this demo, we keep it to two channels, but you can join more channels if you need to.
onCreate() method, we have used requestPermission in order to get the user’s permission for their camera and microphone during the call.
The channel can be any string. The important bit is that if any client wants to talk to another client, they have to be on the same channel.
There is nothing much in the layout. We have two input fields for the channel names and a submit button that redirects us to the calling page.
Creating our Video Calling Layout
Before we start implementing the final method of the application, let’s finish designing the UI for our Video Calling page.
The FrameLayout is the container into which the SurfaceView for the local video is injected. SurfaceView is where Agora usually renders its streams on Android. The RecyclerView will be used to display a grid of videos of the remote users. The Guideline is used to divide the percentage of screen real estate used by each component.
Building the Calling Page
If you have seen our Start a Video Call guide or Start Live Interactive Video Streaming guide, then you know that most of the code that we are writing here is similar to the code we wrote then. The main difference is that earlier we were relying on a single channel to connect a group. But now a single person can join multiple channels simultaneously.
In a single-channel video call, we saw how to create an instance of the RtcEngine class and join a channel. Here, we begin with the same process, which is something like this:
Here we declare two functions —
initAgoraRtcChannelEngineAndJoinChannel() — for adding all the methods related to RtcEngine and RtcChannel. Both functions follow a similar chain of method and event calls, which makes the process easier. So in our
initAgoraEngineAndJoinChannel(), we declare functions like this:
Over here we have divided the process in 4 simple steps:
- Initialization: We initialize our RtcEngine by creating an object named
mRtcEngine, which we will use for all the method calls in this particular class.
- Event Handlers: They look for certain actions or occurrences that happen in a particular stream. For example, the user joins a channel, the user leaves a channel, the user goes offline, or the remote user joins a channel.
onFirstRemoteVideoDecoded() event calls a
setupRemoteVideo() function to display the remote user’s video in the desired location.
onUserOffline() event calls
onRemoteUserLeft(), which removes the stream from our display.
- Set up video profile: Declares how the video needs to be rendered. You can use your own custom configuration.
- Join channel: A channel acts as a common room for people to be in the same video call. The joinChannel() method can be called like this:
This method takes four parameters to run successfully:
- Token: Token authentication is recommended for all RTE apps running in production environments. For more information about token-based authentication in the Agora platform, see https://docs.agora.io/en/Video/token?platform=All%20Platforms.
- Channel Name: It takes a string input to put users into a common video call.
- Optional Info: This is an optional field through which you can pass additional information about the channel.
- uid: It is the unique ID for every user who joins the channel. If you pass 0 or the null value, then Agora automatically assigns a uid for every user.
Note: This project is meant for reference purposes and development environments, it is not intended for production environments. Token authentication is recommended for all RTE apps running in production environments. For more information about token based authentication within the Agora platform please refer to this guide: https://bit.ly/3sNiFRs
This sets up our primary channel or the lobby where our RtcEngine stream can be displayed. Subscribing to other channels requires an instance of RtcChannel. Only then can you join the second channel.
Here, the RtcChannel is initialized with a channel name, so we use the other input given by the user to take care of this. Once it’s initialized, we call the join channel function with the
ChannelMediaOptions() class. This class looks for two parameters:
autoSubscribeVideo. Since it expects a Boolean value, you can pass true or false according to your requirements.
The complete process for RtcChannel can be summarized like this:
Before you build the app, make sure that you did the following:
- You added your App ID while initializing RtcEngine.
- You added your channel name for both RtcEngine and RtcChannel.
- If your project has App Certificate enabled, you used your token in the token variable.
- You followed all the instructions correctly, such as adding the correct dependencies to the build.gradle, adding the permissions to your manifest, and so on.
After you build the app, you should see something like this:
Congratulations! You have implemented your own streaming application built using the Agora Android SDK with the ability to join multiple channels simultaneously.
You can get the complete code for this application here.
And take a look at the complete documentation for the functions discussed above and many more here.
I also invite you to join the Agora.io Developer Slack community.