Add Live Streaming to your Android App using Agora 4.0 SDK Preview
Tags: agora, android, livestreaming, application
Published: 2021-09-17 20:54:36

“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

* An Agora developer account (see [How to get started with Agora](https://www.agora.io/en/blog/how-to-get-started-with-agora))

* Android Studio

* A basic understanding of Android development

**Project Setup**

* Though I won’t be going over the UI building part of the app, I have created a simple UI for our application. You can head over to GitHub to get the design used in this tutorial: [https://github.com/Meherdeep/agora-android-live-streaming/tree/design](https://github.com/Meherdeep/agora-android-live-streaming/tree/design)

![Github Repo For Agora Android Live Streaming](https://cdn-images-1.medium.com/max/2800/0*6_8JvfCrU1wpo-KG)

* 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.

![strings.xml](https://cdn-images-1.medium.com/max/2800/0*KVWyOzfPiIChsslV)

## 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.

{% gist https://gist.github.com/Meherdeep/055e6ad497d905d927656c3f978b91fe %}

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.

 {% gist https://gist.github.com/Meherdeep/2d2bf3c82a7b63e7492e09503323eb94 %}

* 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.

 {% gist https://gist.github.com/Meherdeep/32efca6c2c6c361dc6c419963e10e896 %}

## 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.

 {% gist https://gist.github.com/Meherdeep/226381a7e5460e889e22d6895397fd6b %}

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.

 {% gist https://gist.github.com/Meherdeep/935a3fb3c5688a8b35ae3c274a689a2d %}

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.

  {% gist https://gist.github.com/Meherdeep/7156f71648c30cb8e72c7d603a4f5f5e %}

* 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.

  {% gist https://gist.github.com/Meherdeep/802e4c62431eb747a846d1c924bcc8c4 %}

* 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:

  {% gist https://gist.github.com/Meherdeep/d73dd2eae5ae1ac0fe8c4f890c2e65cf %}

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](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](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.

  {% gist https://gist.github.com/Meherdeep/401a9fd0769479f691357bb1b5e933c3 %}

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.

  {% gist https://gist.github.com/Meherdeep/484f127ff8dd8b8e23e84646f4d08e9f %}

* onRemoteUserLeft : When a remote user leaves a channel this function removes all the views from the container.

  {% gist https://gist.github.com/Meherdeep/2b26e244206d05309b01ca534c462f55 %}

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

  {% gist https://gist.github.com/Meherdeep/aae89d0eb8a4f3bb8e5710ced7d80ac1 %}

The leaveChannel function here calls the stopPreview and leaveChannel methods. 

  {% gist https://gist.github.com/Meherdeep/a4e00e2444933d0890d6241f232268ff %}

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 :

![Testing the Agora Android Live Streaming Application](https://cdn-images-1.medium.com/max/2000/0*S7zo-fEqcvSj8HML.png)

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](https://github.com/Meherdeep/agora-android-live-streaming/tree/ngsdk-update).

## Other Resources

For more information about Agora.io applications, take a look at the [Agora Video Call Quickstart Guide](https://docs.agora.io/en/Video/start_call_android?platform=Android) and[ Agora API Reference](https://docs.agora.io/en/Video/API%20Reference/java/index.html).

And take a look at the complete documentation for the functions discussed above and many more [here](https://docs.agora.io/en/Agora%20Platform/api-ref?platform=Android).

I also invite you to join the [Agora.io Developer Slack community](https://agoraio.slack.com/).