Video Calling

Solution Implementation Guide for Android

Update Time:2020-11-16 11:22

One of the typical uses of a real-time video scene is that multiple users in the same session make a real-time video call. That is, multiple people push and pull each other together, for example:

Taking the scene of a real-time video call between 2 people as an example, the detailed API call sequence diagram of Express Video SDK is as follows:

Please note:

  1. In the above process, the real-time video between 2 room members is taken as an example. In fact, Express Video SDK supports multi-person real-time video. It is recommended that developers refer to the above process to design their own multi-person real-time video call scenes.

  2. In order to make it easier for developers to understand the logic in the Video Call module in Sample Topics, the following sections will pick out and explain the functional core source code fragments. Developers can also directly refer to Demo.

1 Initialize The SDK

Before using Express Video SDK to make a video call, you need to initialize Express Video SDK. Since there are many operations handled internally by the SDK, it is recommended that developers do it when the App starts.

The source code snippets related to initialization in the video call module are demonstrated as follows, the following sample code is not completely consistent with the source code in the video call module, for reference only:

/** Define SDK engine object */
ZegoExpressEngine engine;

/** Fill in appID and appSign */
long appID =; // Please register and get it through the official website, the format is 123456789L
String appSign =; //64 characters, please register through the official website to obtain, the format is "0123456789012345678901234567890123456789012345678901234567890123"

/** Initialize the SDK, use the test environment, and access common scenarios */
engine = ZegoExpressEngine.createEngine(appID, appSign, true, ZegoScenario.GENERAL, getApplication(), null);

2 Log In To The Room And Push And Pull The Stream, Monitor Various Callbacks, And Start A Video Call

Before real-time video conversations between users, they need to log in to the same room. After receiving the callback of successfully logging in to the room, they can directly call the API interface of Express Video SDK for push and pull streaming operations.

A user who has already made a video call in the room needs to obtain a notification of a user who later enters the room for a video call to push the stream, and also needs to obtain a notification of a user who exits the video call halfway to stop the push. After obtaining these notifications, users who previously had a video call in the room should pull the newly added push stream in the room, stop pulling the stopped push stream in the room, and display the corresponding UI.

The source code snippets related to initialization in the video call module are demonstrated as follows, the following sample code is not completely consistent with the source code in the video call module, for reference only:

/** Create user */
ZegoUser user = new ZegoUser("user1");

/** Start logging in to the room */
engine.loginRoom("room1", user);
/** Set and listen for callback */
engine.setEventHandler(new IZegoEventHandler() {

    /** The following are commonly used room-related callbacks */

    @Override
    public void onRoomStateUpdate(String roomID, ZegoRoomState state, int errorCode, JSONObject extendedData) {
        /** Room status update callback. After logging in to the room, when the room connection status changes (such as room disconnection, login authentication failure, etc.), the SDK will notify you through this callback */
        //....
    }

    @Override
    public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
        /** The user status is updated. After logging in to the room, when a user is added or deleted in the room, the SDK will notify via this callback */
        //....
    }

    @Override
    public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList) {
        /** The stream status is updated. After logging in to the room, when a user pushes or deletes a new audio and video stream in the room, the SDK will notify you through this callback */
        if(updateType == ZegoUpdateType.ADD){
                for(ZegoStream zegoStream: streamList){
                    AppLogger.getInstance().i("onRoomStreamUpdate: ZegoUpdateType.ADD streamId:"+zegoStream.streamID);
                    TextureView addTextureView=new TextureView(ZGVideoTalkUI.this);
                    int row=streamIdList.size()/2;
                    int column=streamIdList.size()%2;
                    addToGridLayout(row,column,addTextureView);
                    viewMap.put(zegoStream.streamID,addTextureView);
                    streamIdList.add(zegoStream.streamID);
                    mSDKEngine.startPlayingStream(zegoStream.streamID, new ZegoCanvas(addTextureView));
                }
            }else if(updateType == ZegoUpdateType.DELETE){// callback in UIThread
                for(ZegoStream zegoStream: streamList){
                    AppLogger.getInstance().i("onRoomStreamUpdate: ZegoUpdateType.DELETE streamId:"+zegoStream.streamID);
                    mSDKEngine.stopPlayingStream(zegoStream.streamID);
                    streamIdList.remove(zegoStream.streamID);
                    notifyGridLayout();
                    viewMap.remove(zegoStream.streamID);
                }
            }
     }

     //....
});

In the onRoomStreamUpdate callback, we can get the stream addition/removal information, pull the stream to play startPlayingStream when the stream is added, and stop pulling the stream when the stream is removed stopPlayingStream

Please refer to the room login process: Quick Start-Login

For the push flow process, please refer to: Quick Start-Publishing

Note:

  1. The onPublisherStateUpdate callback can monitor the push stream status. If the push is unsuccessful, it is generally a network problem, and the SDK will do retry work. Developers can also make a limited number of push retry according to the situation, or give a friendly interactive prompt.

Please refer to the pull flow process: Quick Start-Playing

Note:

  1. onPlayerStateUpdate can monitor the streaming state. If the streaming is unsuccessful, it is usually a network problem, and the SDK will do retry work. Developers can also do a limited number of streaming retries according to the situation, or give a friendly interactive prompt.

3 Stop Video Call

When the user exits the video call during the video call, he should stop streaming, stop streaming, exit the room, and release the corresponding UI object resources.

The source code snippets related to initialization in the video call module are demonstrated as follows, the following sample code is not completely consistent with the source code in the video call module, for reference only:

/** Stop streaming */
engine.stopPublishingStream();
/** Stop local preview */
engine.stopPreview();

for(String playStreamid: playStreamids){
    // Stop pulling multiple streams, where the developer should maintain the list of pulled streams in the business layer, such as playStreamids here
    // stop streaming
engine.stopPlayingStream(streamID);
    // Set the corresponding pull-stream rendering View to null, release resources, etc.
    ...;
}
/** Log out of the room */
engine.logoutRoom("room1");