09/17/2021 | News release | Distributed by Public on 09/17/2021 09:35
'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!
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:
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.
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
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.
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.
Before you build the app, make sure that you did the following:
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.
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.
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.
Want to build Real-Time Engagement apps?
Get started with 10,000 free minutes today!
If you have questions, please call us at 408-879-5885. We'd be happy to help you add voice or video chat, streaming and messaging into your apps.