Live stream in Android using ZEGOCLOUD

·

5 min read

Influencers and entertainers have always been able to interact with their fans in a variety of ways, but recently, some have taken things a step further.

Influencers nowadays commonly use Live streaming features on different Apps to interact with their fans and audience. Apart from the physical events, social and live streaming interactions become common. Fans can ask questions to them and much more.

While developing an app in Android which has a Live streaming feature, it's a bit difficult to find any third-party library which not just has only live streaming but also it should provide good quality video/audio, pricing should be good, and much more.

I have tried a lot of libraries but they are not up to the mark as they are too expensive or not providing good-quality video/audio.

Let me tell you about a library ZEGOCLOUD, which can perfectly match this live-streaming feature.

ZEGOCLOUD is a library of best-quality video, audio, and photo content. It also provides a Live streaming feature with many options like co-hosting, streaming with chatting, ultra-low latency connections, and much more.

Superior audio and video quality: ZEGOCLOUD provides a multi-quality of audio and video, and you can adjust the quality rate according to the network.

Smooth communications with ultra-low latency When it comes to network stability and connectivity, ZEGOCLOUD provide great quality video and audio even when your network latency is ultra-low, So you don't need to worry about disconnectivity issue.

Use Cases

Social and entertainment: Social entertainment become part of our life and to be connected ZEGOCLOUD offer many features like single host, multiple host,s and various other eye-catching features like face beautification, voice beautification, and virtual gifting

Live shopping: Transform the experience of shopping into live to stream, interact with customers, guide them with product experts, and give them a feeling of a physical store.

Live game streaming: The world is moving so fast and with that now you can interact with your famous gammers just at your home with Live Streaming. Now almost all famous gamers have adopted that habit and do live streaming,

To add the permissions open the file using path app/src/main/AndroidManifest.xml and add the following code

<!-- Permissions required by the SDK -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<!-- Permissions required by the App -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Note: If the user has Android 6.0. or later then some permissions need to be requested at runtime rather than you declared them statically, for the add the following code in AndroidMainfest.xml

String[] permissionNeeded = {
    "android.permission.CAMERA",
    "android.permission.RECORD_AUDIO"};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED ||
        ContextCompat.checkSelfPermission(this, "android.permission.RECORD_AUDIO") != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(permissionNeeded, 101);
    }
}

Prevent ZEGO SDK public class name obfuscation y adding this code in file proguard-rules.pro.

-keep class **.zego.**{*;}

Implement a basic live streaming

Create a singleton instance of ZegoExpressEngine class, and call the createEngine method with required params, AppId, and appSign, both can be get from the project dashboard

//  Create a ZegoExpressEngine instance and set eventHandler to [self]. 
// If eventHandler is set to [null], no callback will be received. You can set up the event handler later by calling the [setEventHandler:] method.
ZegoEngineProfile profile = new ZegoEngineProfile();
profile.appID = ;  // The AppID you get from ZEGOCLLOUD Admin Console. AppID format: 123456789L
profile.appSign = ;  // The AppSign you get from ZEGOCLOUD Admin Console. AppSign format: @"0123456789012345678901234567890123456789012345678901234567890123" (64 characters)
profile.scenario = ZegoScenario.GENERAL;  // Use general scenario.
profile.application = getApplication();
engine = ZegoExpressEngine.createEngine(profile, null);

Create a user with ZegoUser method, and create an instance of ZegoroomConfig, can generate a token from the project dashboard.
then login to the room using the engine.loginRoom method.

/** create a user */
ZegoUser user = new ZegoUser("user1");


ZegoRoomConfig roomConfig = new ZegoRoomConfig();
/** Token is generated by the user's own server. For an easier and convenient debugging, you can get a temporary token from the ZEGOCLOUD Admin Console */
roomConfig.token = "xxxx";
/** onRoomUserUpdate callback can be received only by passing in a ZegoRoomConfig whose "isUserStatusNotify" parameter value is "true".*/
roomConfig.isUserStatusNotify = true;

/** log in to a room */
engine.loginRoom("room1", user, roomConfig, (int error, JSONObject extendedData)->{
    // (Optional callback) The result of logging in to the room. If you only pay attention to the login result, you can use this callback.
});

Add some listeners to get updates on the room.

engine.setEventHandler(new IZegoEventHandler() {

    /** Common event callbacks related to room users and streams. */

    /** Callback for updates on the current user's room connection status. */
    @Override
    public void onRoomStateUpdate(String roomID, ZegoRoomState state, int errorCode, JSONObject extendedData) {
        /** Implement the callback handling logic as needed. */
    }

    /** Callback for updates on the status of other users in the room. */
    @Override
    public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
        /** Implement the callback handling logic as needed. */
    }

    /** Callback for updates on the status of the streams in the room. */
    @Override
    public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList, JSONObject extendedData){
        /** Implement the callback handling logic as needed. */
     }

});

Stop publishing and playing streams
stopPublishingStream method will call to stop publishing a local audio or video stream to remote users

/** Stop publishing a stream */
engine.stopPublishingStream();

stopPreview method will be called to stop local video preview when needed

/** Stop local video preview */
engine.stopPreview();

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

/** Stop playing a stream*/
engine.stopPlayingStream(streamID);

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.

/** Log out of a room */
engine.logoutRoom("room1");

When you leave the room destroy the instance.

/** Destroy the ZegoExpressEngine instance */
ZegoExpressEngine.destroyEngine(null);

You can get the sample code from here

You can get the integration steps from here