ZEGOCLOUD easy example

Platform ZEGOCLOUD

Click the search button below to search documentation or error code

image

ZEGOCLOUD's easy example is a simple wrapper around our RTC product. You can refer to the sample code for quick integration.

Getting started

Prerequisites

Clone the repository

  1. Clone the easy example Github repository.
  2. Checkout to call_invite branch

Setup FCM

  1. Go to Firebase Console and create new project if you don't have one.
  2. Andd new Android app to your Firebase project. Download the google-service.json file and move it into your Android app module root directory.
  3. Add the google-services plugin as a dependency inside of your /android/build.gradle file:
buildscript {
  dependencies {
    // ... other dependencies
    classpath 'com.google.gms:google-services:4.3.10'
    // Add me --- /\
  }
}
  1. Execute the plugin by adding the following to your /android/app/build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services' // <- Add this line

Modify the project configurations

  • You need to modify appID to your own account, which can be obtained in the ZEGO Admin Console.
  • You need to set serverUrl to a valid URL that can be obtained for Zego auth token and post FCM notification request.

Run the sample code

  1. Connect the Android device to your computer.

  2. Open Android Studio, select the Android device you are using,click the Run 'app' in the upper center to run the sample code and experience the Live Audio Room service.

Integrate into your own project

Introduce SDK

In your setting.gradle file, add the jitpack maven .

pluginManagement {
    repositories {
        
        maven { url 'https://www.jitpack.io' } // <- Add this line
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven { url 'https://www.jitpack.io' } // <- Add this line
    }
}

Import the source code module

import the :zegoexpress module to your project

Choose the zegoexpress directory. And add dependency in your app's build.gradle's dependencies:

dependencies{
   implementation project(':zegoexpress') 
}

Setup FCM

Same as getting started section.

Setup backend service

  1. Generate Firebase Admin SDK Private Key

Generate Key 2. Click this deploy button to start deploy your service:

Deploy with Vercel

If you are using Firebase Cloud Functions, check this doc for usage and check this example code to make the FCM work with your project.

Method call

The calling sequence of the SDK interface is as follows: createEngine --> joinRoom --> setLocalVideoView/setRemoteVideoView --> leaveRoom

Create engine

Before using the SDK function, you need to create the instance of the SDK(Engine) first. We recommend creating it when the application starts. The sample code is as follows:

 ExpressManager.getInstance().createEngine(getApplication(), AppCenter.appID);

Join room

When you want to communicate with audio and video, you need to call the join room interface first.

If you need to invite other to join the call, you can send the invitation at the same time while you join the room.

binding.callUser.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        PermissionX.init(LoginActivity.this)
            .permissions(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
            .request((allGranted, grantedList, deniedList) -> {
                if (allGranted) {
                    CloudMessage cloudMessage = new CloudMessage();
                    cloudMessage.targetUserID = binding.targetUserId.getText().toString();
                    cloudMessage.roomID = selfID;
                    cloudMessage.callType = "Video";
                    cloudMessage.callerUserID = selfID;
                    cloudMessage.callerUserName = selfName;
                    cloudMessage.callerIconUrl = selfIcon
                    HttpClient.getInstance().callUserByCloudMessage(cloudMessage, new HttpResult() {
                        @Override
                        public void onResult(int errorCode, String result) {
                            if (errorCode == 0) {
                                joinRoom(cloudMessage.roomID, cloudMessage.callerUserID,
                                    cloudMessage.callerUserName);
                            } else {
                                Toast.makeText(getApplication(), "callUserByCloudMessage failed:" + result,
                                    Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }
            });
    }
});

set video view

If your project needs to use the video communication function, you need to set the View for displaying the video, call setLocalVideoView for the local video, and call setRemoteVideoView for the remote video.

setLocalVideoView:

ExpressManager.getInstance().setLocalVideoView(binding.localTexture);

setLocalVideoView:

@Override
public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
    if (updateType == ZegoUpdateType.ADD) {
        for (int i = 0; i < userList.size(); i++) {
            ZegoUser user = userList.get(i);
            TextureView remoteTexture = binding.remoteTexture;
            binding.remoteName.setText(user.userName);
            setRemoteViewVisible(true);
            ExpressManager.getInstance().setRemoteVideoView(user.userID, remoteTexture);
        }
    } else {
        setRemoteViewVisible(false);
    }
}

leave room

When you want to leave the room, you can call the leaveroom interface.

ExpressManager.getInstance().leaveRoom();

How is call invitation works

  1. Device A starts the App to obtain the FCM Token, sends a POST request to https://server/store_fcm_token, and stores the UserID_A and FCM Token on the Server.

  2. Device B starts the App to obtain the FCM Token, sends a POST request to https://server/store_fcm_token, and stores the UserID_B and FCM Token on the Server.

  3. The App on Device B is killed.

  4. Device A sends the call_invitation_data by sending an HTTP request to https://server/send_call_invitation, and calls the loginRoom and startPublishingStream methods of ZegoExpressEngin to start the call.

  5. The Server sends a message to Device B via the Firebase messaging.send interface.

  6. Device B receives the call_invitation_data via the public void onMessageReceived(@NonNull RemoteMessage remoteMessage) interface and pops up the phone call invitation dialog.

  7. After clicking the system notification box, Device B starts the App and uses the call_invitation_data to call the loginRoom and startPublishingStream methods of ZegoExpressEngin to start the call.