Voice Calling

Solution Implementation Guide for Android

Update Time:2020-11-16 11:22

1 Function Description

One of the typical real-time voice scenarios is that members in the same room have real-time voice conversations.

2 Flow Description

Taking real-time voice between 2 people as an example, the main process is as follows:

Caution:

In the above process, the real-time voice between two room members is used as an example. In fact, the Zego SDK supports real-time voice for multiple people. It is recommended that developers design as needed.

The detailed steps are as follows: 1.Initialize SDK: Only after the SDK is initialized can login and push-pull streaming operations be performed. 2.Set callbacks: including room status callback, push flow callback, pull flow callback. 3.Log in to the room. 4.Push audio stream: Push local audio in real time. 5.Pull audio stream: Play audio of other users in the room. 6.Exit the room: To exit the room, you need to stop pushing and pulling, and then log out of the room.

3 Code Description

3.1 Initialization SDK

/** definition SDK engine object */
ZegoExpressEngine engine;

/** fill in appID and appSign */
long appID = ;  // Please register through the official website, the format is:123456789L
String appSign = ;  //64 characters, please register through the official website, the format is"0123456789012345678901234567890123456789012345678901234567890123"

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

Quick Starts-Integrating the SDK

3.2 Set Callbacks

ZegoExpressEngine engine; // SDK engine object
...
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 login 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 into the room, when a user is added or deleted in the room, the SDK will notify you through this callback */
        //....
    }

    @Override
    public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList) {
        /** The stream status is updated. After logging into the room, when a user pushes or deletes an audio stream in the room, the SDK will notify you through this callback */
        //....
     }

    /** The following are commonly used push streams related callbacks */

    @Override
    public void onPublisherStateUpdate(String streamID, ZegoPublisherState state, int errorCode, JSONObject extendedData){
        /** After the push streaming interface is successfully called, when the state of the push streamer changes, such as a network interruption causing abnormal push flow, the SDK will notify this callback when retrying the push flow */
        //....
    }

    /** The following are commonly used pull streams related callbacks */

    @Override
    public void onPlayerStateUpdate(String streamID, ZegoPlayerState state, int errorCode, JSONObject extendedData){
        /** After the pull streams interface is successfully called, when the state of the pull streamer changes, such as a network interruption causing abnormal flow pushing, the SDK will notify this callback when retrying the pull stream */
        //....
    } 

    //....
});

3.3 Logging in to a Room

Before conducting real-time voice conversations between users, they must first log in to the same room.

Note: The room ID only supports strings with a maximum length of 128 bytes. The format only supports numbers, English characters and'~','!','@','#','$','%','^ ','&','*','(',')','_','+','=','-','`',';',''',',', '.','<','>','/','' special characters.

ZegoExpressEngine engine; // SDK engine object
...
/** Create user object */
ZegoUser user = new ZegoUser("user1");

/** Start logging into the room */
engine.loginRoom("room1", user);

Quick Starts-Login

3.4 Start Pushing Audio Stream

After successfully logging into the room, you can push the audio stream. Developers can call startPublishingStream to push the stream. If you do not need to continue pushing the stream, please call stopPublishingStream to stop pushing the stream.

Note: streamID only supports character strings no longer than 256, and it needs to be globally unique within the entire AppID. If it appears in the same AppID, different users each pushed a stream and the stream name is the same, which will cause the user to push the stream back Push streaming failed. URL keywords cannot be included, otherwise the push/pull streams fails. The format only supports numbers, English characters and'~','!','@','#','$','%','^','&','*','(',') ','_','+','=','-','`',';',''',',','.','<','>','/', '' Special characters.

ZegoExpressEngine engine; // SDK engine object
...
/** start push stream */
engine.startPublishingStream("stream1");

Quick Starts-Publishing

3.5 Play audio stream

After successfully logging into the room, the live audio stream can be played. Developers can call startPlayingStream to pull the stream. If you do not need to continue to pull the stream, please call stopPlayingStream to stop the stream.

*Note: streamID only supports character strings up to 256 in length. URL keywords cannot be included, otherwise the push-pull flow fails. The format only supports numbers, English characters and'~','!','@','#','$','%','^','&','','(',') ','_','+','=','-','`',';',''',',','.','<','>','/', '' Special characters.**

ZegoExpressEngine engine; // SDK engine object
...
/**
 *  Start pull stream, the stream name is obtained from the onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList) callback after logging in the room, and this callback will be received when there are new/deleted streams
 */
engine.startPlayingStream("stream1");

Quick Starts-Playing

3.6 End audio live broadcast

After the audio call ends, the operations are mainly to log out of the room, stop pushing the stream, stop pulling the stream, clear the view or data, etc., and determine whether the SDK needs to be released according to the business needs.

ZegoExpressEngine engine; // SDK engine object
...
/** stop push Stream */
engine.stopPublishingStream();
/** stop push Stream */
engine.stopPlayingStream(streamID);
/** Log out of the room */
engine.logoutRoom("room1");

/** If you do not need to continue to use the SDK, you can call the following function to release the SDK. 
 * Note: When the SDK needs to be used again after the SDK is released, the SDK must be re-initialized.
 */
ZegoExpressEngine.destroyEngine(null);