In this tutorial, we will explore how to build a live-streaming app that allows users to host and view live events virtually. We will use AmityStream and ExoPlayer to enable seamless live streaming and playback of recorded streams. AmityStream is a powerful platform that provides the necessary tools to manage live streams, while ExoPlayer, a media player library from Google, offers advanced features and adaptability for smooth playback. Once you have achieved this, you can create a Live Stream Post following the details in the following tutorial: How to create a Live Stream Post for Android.
To start working with streams, we need to initialize the <span id="greylight" class="greylight">AmityStreamRepository</span> first. It acts as a central point to interact with the live-streaming platform.
Retrieving a Stream Object
Each live stream has a unique identifier, and we can retrieve a single stream object using its ID.
Checking Stream Status
A stream can have various statuses, such as IDLE, LIVE, ENDED, or RECORDED. To check the status of a stream, we can use the <span id="greylight" class="greylight">getStatus()</span> method.
Stream Status: Understanding the Lifecycle of a Stream
In the AmityStream platform, a stream can exist in various states, which change based on events and actions during its lifecycle. These states are represented by the Amitystream.Status enum. Let’s explore each possible status:
- IDLE: A stream in the IDLE state has been created but hasn’t started broadcasting yet. It’s like a waiting room for the live event.
- LIVE: A stream in the LIVE state is currently being broadcasted and is actively accessible to viewers. It’s happening in real-time.
- ENDED: A stream in the ENDED state has concluded its live broadcast and is now transitioning to become a recorded stream.
- RECORDED: A stream in the RECORDED state has finished its live broadcast and has been successfully transformed into a recorded stream. It’s ready for playback.
You can easily check the status of a stream by calling the <span id="greylight" class="greylight">getStatus()</span> method on the AmityStream object. This will return one of the <span id="greylight" class="greylight">Amitystream.Status</span> enum cases, allowing you to manage and display the stream’s status accordingly in your application.
Retrieving a Stream Collection
We can query a collection of live streams based on their statuses. The following code demonstrates how to observe changes in the collection using <span id="greylight" class="greylight">RxJava</span>.
Playing a Live Stream
To play a live stream, we need to obtain the URL for playback using the <span id="greylight" class="greylight">getWatcherData().getUrl()</span> method from the stream object.
Note: Make sure to use ExoPlayer with the <span id="greylight" class="greylight">DefaultDataSourceFactory</span> for FLV streams and <span id="greylight" class="greylight">RtmpDataSourceFactory</span> for RTMP streams, as shown earlier.
Playing a Recorded Stream
To play a recorded stream, we can use a similar approach, but this time we use <span id="greylight" class="greylight">getRecordings()[index]</span> from the stream object to get the URL.
Recommended Video Player — ExoPlayer
We highly recommend using ExoPlayer, Google’s powerful media player library, for playing live and recorded streams. ExoPlayer supports various adaptive playback formats like DASH and SmoothStreaming, providing a seamless user experience. Here’s how you can use ExoPlayer with <span id="greylight" class="greylight">DefaultDataSourceFactory</span> for FLV and <span id="greylight" class="greylight">RtmpDataSourceFactory</span> for RTMP streams.
By using AmityStream and ExoPlayer, you can build a powerful live streaming app that allows users to host and view events in real-time. AmityStream takes care of managing the live streams, while ExoPlayer provides seamless playback of live and recorded streams with support for various adaptive playback formats.
Understanding the different stream statuses is crucial for efficiently handling live events and enabling smooth transitions between live and recorded streams for your users. With this knowledge, you can build a more robust live streaming app that provides an engaging experience to your audience.