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.
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.
build.gradle (app level)
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.
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.
MainActivity.java
In the 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.
activity_main.xml
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.
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.
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:
VideoActivity.java
Here we declare two functions — initAgoraEngineAndJoinChannel()
and 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:
mRtcEngine
, which we will use for all the method calls in this particular class.Event Handlers
Here the onFirstRemoteVideoDecoded()
event calls a setupRemoteVideo()
function to display the remote user’s video in the desired location.
The onUserOffline()
event calls onRemoteUserLeft()
, which removes the stream from our display.
setupVideoProfile()
This method takes four parameters to run successfully:
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: autoSubscribeAudio
and 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:
RtcChannel
Before you build the app, make sure that you did the following:
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.
For more information about Agora.io applications, take a look at the Agora Video Call Quickstart Guide and Agora API Reference.
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 Developer Slack community.