How to build a live streaming app?
Tags: java, android, mobile, programming
Published: 2022-07-29 08:18:00

## What is a Live Streaming App?
[Live streaming](https://www.zegocloud.com/product/live-streaming) technology allows you to broadcast, watch, create and share videos in real-time. All you need to be able to live stream is an internet-enabled device, like a smartphone or tablet, and a platform (such as a website or app) to live stream.

Live streaming apps are rapidly developing, given this trend's tremendous opportunities in many fields. Depending on the domain, the basic requirements for building a live streaming app differ as follows:

1. **Game live streaming app** - The picture must be clear and smooth to construct a game live streaming app.
2. **Entertainment live streaming app** - Building an entertainment app requires various interactive functions such as gifts, barrage, and mini-games.
3. **Shopping live streaming app** - It must provide functions such as shopping cart and live broadcast playback.
4. **Sports live streaming app** - It is necessary to ensure a clear and smooth picture and provide a variety of resolutions for users to choose from.
5. **Educational live streaming app** - Interactive whiteboard, file sharing, screen sharing, and other functions are essential to delivering the best teaching experience to students. 

## Must-Have Features Of a Live Streaming app
Despite the differences, there are some standard functions that all live streaming apps should have, such as:

- **Client join/sign-in** - A straightforward enrollment structure for the clients to make a record and sign in to the application with the certification.

- **Search** -  A search box allowing users to search content by topic, popularity, trending, channel, location, or interests.

- **Client profile** - Show the client's data and profile picture to companions and subscribers.

- **Live streaming** - The app's core permits the client to record and communicate a live stream to individuals who subscribe to their channel or the public.

- **Chats** - Adding a chat function helps the host communicate with the audience in real-time.

[Building a live streaming app](https://www.zegocloud.com) in different fields will require integrating some unique features. Relevant examples are:

- **Screen Sharing** - For a game live streaming app, you must share the game screen with the audience through the screen sharing function.

- **Host PK** - Required for the entertainment live streaming apps. Through the PK between anchors, one can enrich the content of the live streaming app, enhancing the audience's enthusiasm by sending gifts.

- **Shopping Cart** - The live shopping streaming app provides a shopping portal for viewers to place orders and purchase products directly in the live streaming room.

- **Share whiteboard or file** - The education live streaming app must share various teaching courseware, so providing functions such as shared whiteboard and transferred files to enrich the teaching form is necessary.

## What is a Live Streaming SDK?
The Live Streaming App is complex, especially if each function is self-developed. It requires a vast technical team and consumes many human and financial resources.

Many companies encapsulate the standard functions of Live Streaming apps into SDK, thus, developing a Live Streaming app only needs to focus on business interaction development.

Among these functions:

- **[Express SDK](https://www.zegocloud.com/product/live-streaming?_source=dev&article=26)** - Audio and video transmission service SDK encapsulates audio and video capture, preprocessing, encoding, transmission, decoding, rendering, and so on.

- **[ZIM SDK](https://www.zegocloud.com/product/in-app-chat?_source=dev&article=26)** - Message communication SDK, which encapsulates the transmission, and broadcasting of text messages, picture messages, video messages, and other messages, ability to notify.

- **[SuperBoard SDK](https://www.zegocloud.com/product/super-board?_source=dev&article=26)** - Encapsulates interactive whiteboard, file management, file rendering, and other capabilities.

- **[RoomKit SDK](https://www.zegocloud.com/solutions/education?_source=dev&article=26)** - Encapsulates a complete educational scene, including audio and video calls, barrage messages, shared whiteboards, shared files, equipment management, student management and other teaching scene logic.

## Steps to Build a Live Streaming App

### Prerequisites

Before you begin, make sure you complete the following steps:

- Create a project in [ZEGOCLOUD Console](https://console.zegocloud.com?_source=dev&article=26), and get the AppID of your project.
- The ZEGO Express SDK has been integrated into the project. For details, see [Integration](https://docs.zegocloud.com/article/7942?_source=dev&article=26).

### Implementation process

The following diagram shows the basic process of User A playing a stream published by User B: 

![process of playing a stream](https://resource.zegocloud.com/content_resource/2022/07/29/16589145548257.png)

The following sections explain each step of this process in more detail.


### Create a ZegoExpressEngine instance

To create a singleton instance of the `ZegoExpressEngine` class, call the `createEngine` method with the **AppID** of your project.

{% embed https://gist.github.com/larryluoo/3b8b8819c6d30a66242db6a507b17d10 %}

### Login to a room

To log in to a room, call the `loginRoom` method.

{% embed https://gist.github.com/larryluoo/a3fc3dc6f383a3d078f585534e0b7732 %}

Then, to listen for and handle various events that may happen after logging in to a room, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to room users and streams:

{% embed https://gist.github.com/larryluoo/943deef217c1b09b086b652f44e914d3 %}

### Start the local video preview

To start the local video preview,  call the `startPreview` method with the view for rendering the local video passed to the `canvas` parameter.

You can use a `SurfaceView`, `TextureView`, or `SurfaceTexture` to render the video.

{% embed https://gist.github.com/larryluoo/a4cc0c566dfa6497f8f28194547c240d %}

### Publish streams

To start publishing a local audio or video stream to remote users, call the `startPublishingStream` method with the corresponding Stream ID passed to the `streamID` parameter.     

{% embed https://gist.github.com/larryluoo/4a3a894a017e47ca23fcc74df89c8655 %}

Then, to listen for and handle various events that may happen after stream publishing starts, you can implement the corresponding event callback methods of the event handler as needed. The following is a common event callback related to stream publishing:

{% embed https://gist.github.com/larryluoo/4047ecb78cf4c67737ce9b82995ae316 %}

### Play streams

To start playing a remote audio or video stream, call the `startPlayingStream` method with the corresponding Stream ID passed to the `streamID` parameter and the view for rendering the video passed to the `canvas` parameter.


You can obtain the stream IDs of the streams published by other users in the room from the callback `onRoomStreamUpdate`.

You can use a `SurfaceView`, `TextureView`, or `SurfaceTexture` to render the video.

{% embed https://gist.github.com/larryluoo/7d10399777882a72e54351c770ee09ac %}

### Stop publishing and playing streams

To stop publishing a local audio or video stream to remote users, call the `stopPublishingStream` method.

{% embed https://gist.github.com/larryluoo/6aae068cf4af2554f0c71a2d639490be %}

If local video preview is started, call the `stopPreview` method to stop it as needed.

{% embed https://gist.github.com/larryluoo/2c4364e0252b2f8360b393885d5b76d3 %}

To stop playing a remote audio or video stream, call the `stopPlayingStream` method with the corresponding stream ID passed to the `streamID` parameter.

{% embed https://gist.github.com/larryluoo/eda9e93738daa5eb6b9c7b67954f568d %}

### Log out of a room

To log out of a room, call the `logoutRoom` method with the corresponding room ID passed to the `roomID` parameter.

{% embed https://gist.github.com/larryluoo/9da3aa9d9fbbe5ef4d98073af5a9be10 %}

### Destroy the `ZegoExpressEngine` instance

To destroy the `ZegoExpressEngine` instance and release the resources it occupies, call the `destroyEngine` method.

{% embed https://gist.github.com/larryluoo/fcebfac35be45e2760cf069c2869f3c8 %}

### API call sequence diagram

The following diagram shows the API call sequence of the stream publishing and playing process: 

![API call sequence](https://resource.zegocloud.com/content_resource/2022/07/29/16589152502137.png)

---

[Sign up](https://console.zegocloud.com/account/signup?_source=dev&article=26) with ZEGOCLOUD, and get **10,000 minutes** free every month.

## Did you know? 👏

> **Like** and **Follow** is the biggest encouragement to me
> **Follow me** to learn more technical knowledge
> Thank you for reading :)

## Learn more

This is one of the live technical articles. Welcome to other articles:

{% embed https://dev.to/zegocloud/how-to-build-a-clubhouse-clone-app-with-android-and-zegocloud-a-social-audio-app-development-2pod %}

{% embed https://dev.to/davidrelo/movies-together-online-in-a-few-hours-source-code-inside-40o0 %}

{% embed https://dev.to/davidrelo/improve-live-streaming-experience-with-stream-mixing-345o %}

{% embed https://dev.to/zegocloud/how-to-implement-a-video-call-app-using-ios-callkit-and-zegocloud-478g %}