Skip to content
Add Live Streaming to Your Android App Using the Agora SDK - Featured

Add Live Streaming to your Android App using Agora 4.x SDK Preview

By Author: Meherdeep Thakur In Developer

“You’re live!” — that’s a phrase that used to be limited to celebrities and other famous people. But now we see everyone going live and sharing their life stories. We have seen the market evolve so much that live streaming has become one of the biggest sources of real-time engagement (RTE).

Building your own live streaming application can be difficult especially if scalability is a concern. Agora makes our lives much easier by providing an easy-to-use SDK that also gives developers low-level control of exactly how we want the streaming service to work. While using the Agora platform, you don’t have to worry about scalability, call drops, or latency.

In this tutorial, we will walk through how to add live streaming to an Android application with multiple broadcasters and audiences who can subscribe to their stream.

So let’s get started!

Requirements

Project Setup

  • Navigate to the strings.xml file, and add your App ID to the private_app_id resource. Make sure to then sync your Gradle files.

MainActivity.java

Now, let’s create a file called MainActivity.java. This file contains the logic of what the users see when they first open the app. The main UI elements include a radio button and a text box. The radio button allows a user to decide whether they want to be a host or an audience for the live stream. The text box allows the user to input the channel name they wish to join.

In our onCreate method, we add the user permission so that the application asks for camera and microphone permissions as soon as the page is built.

We’ll be adding two methods to our class:

  • The first method will be called when the user chooses an option from the radio buttons. We will be setting a variable accordingly. We will be setting it to a value that will determine whether the user is a broadcaster or an audience.
  • We then implement a function that is called when the user submits the details. Here we will be getting all the details we need and sending them over to the next activity.

VideoActivity.java

By default, audiences have their cameras disabled and microphones muted, but the broadcasters have complete access. So when users join as a broadcaster, their videos are automatically added to the interface.

So first we are adding some variables that we will be using throughout the class. We then modify the onCreate method to get the data that we had passed in from the previous activity.

We declare a method called initAgoraEngineAndJoinChannel that will call all the other methods needed in the life cycle of the broadcast. We also define the event handler that will decide what methods to call when a remote user joins or leaves or when the remote feed is muted.

Let’s take a deeper look into the initAgoraEngineAndJoinChannel() method and all the other methods referred in it.

  • initializeAgoraEngine(): This is a method used to initialize the RtcEngine.
  • setClientRole(): This method sets the role of a user to host or an audience (default). This method should be called before joining the channel. You can call it again after joining the channel to switch the client role.
  • setupLocalVideo(): This function is used to refer to the setupLocalVideo method from our AgoraRtcEngine, through which we set a surface view for our local user that is used in the live stream.
  • startPreview: This method allows you to add your local stream before joining a channel. It is important that before calling this method we have enabled the video and set up our local user video.
  • joinChannel(): 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

In our initialize function, we passed mRtcEventHandler as one of the parameters to the create method. This sets up a series of callback events that are triggered whenever a user joins a channel or leaves a channel.

Using these callbacks we are calling functions as a user joins or leaves the channel.

  • setupRemoteVideo: When a remote user joins the call the remote container is selected to display the video of the remote user. Here we will be using FrameLayout to display the user video.
  • onRemoteUserLeft: When a remote user leaves a channel this function removes all the views from the container.

We then add the onDestroy method for releasing the resources that we have used.

The leaveChannel function here calls the stopPreview and leaveChannel methods.

You can also add features like switching the camera by calling the switchCamera() method using our RtcEngine’s object:

mRtcEngine.switchCamera()

Or muting the microphone by calling the muteLocalAudioStream() method using our RtcEngine’s object:

mRtcEngine.muteLocalAudioStream(true)

With this, you’re ready to go and build your own live streaming application.

Testing

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 file, adding the permissions to your manifest, and so on.

After you build the app, you should see something like this:

Here I have added two broadcaster streams while multiple audiences have subscribed to it.

Conclusion

Congratulations! You have implemented your own live video streaming application built using the Agora Android SDK.

You can get the complete code for this application here.

Other Resources

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.