I. Prerequisites

Before implementing Java live, make sure that:

  • The ZEGO Express SDK has been integrated into the project, see Quick Start – Integration for details.

  • A project has been created in the ZEGO console and a valid AppID and AppSign have been requested. For details, see Console – Project Management.

Second, the implementation process

The implementation process of Java live broadcast introduced in this paper is based on ZEGO Express SDK. The following is the API call timing diagram:

1. Create engine

1) Create interface (optional)

Before you begin, it is recommended that developers add the following interface elements to facilitate basic real-time audio and video functionality.

  • Local Preview Window

  • Remote video window

  • The end of the button

2) Create the engine

Define the SDK engine object, call the createEngine interface, pass the AppID and AppSign to the parameters “AppID” and “AppSign”, and create the engine singleton object.

If you need to register a callback agent, you can pass the object that implements IZegoEventHandler as the parameter “eventHandler”. If you do not need to register the callback agent, you can pass “null” to the parameter “eventHandler”. If you still need to register the callback after creating the engine, you can set the callback agent by calling the setEventHandler interface.

Plain Text

/** Define the SDK engine object */

ZegoExpressEngine engine;

/** Enter appID and appSign */

long appID = ; /** Please register on the official website to obtain it in the format of 123456789L */

String appSign = ; / * * 64 characters, please register through the website to receive, format for “0123456789012345678901234567890123456789012345678901234567890123” * /

/** Create engine, use test environment, common scenario access */

engine = ZegoExpressEngine.createEngine(appID, appSign, true, ZegoScenario.GENERAL, getApplication(), null);

Log in to the room

1) login

After creating the ZegoUser user object, call the loginRoom interface, pass in the roomID parameter “roomID” and the user parameter “user”, login to the room.

Note that:

  • In the same AppID, ensure that roomID is globally unique.

  • In the same AppID, ensure that userID is globally unique. Developers are advised to set userID to a meaningful value so that userID can be associated with their own business account system.

  • The ZegoUser constructor public ZegoUser(String userID) sets “userName” to be the same as the “userID” parameter passed. The values of userID and userName cannot be NULL. Otherwise, the login to the room fails.

Plain Text

/** Create user */

ZegoUser user = new ZegoUser(“user1”);

/** Start logon room */

engine.loginRoom(“room1”, user);

2) Listener event callback (optional)

According to the actual application needs, after logging in to the room, listen for notifications of events that you want to pay attention to, such as room status update, user status update, flow status update, etc.

  • OnRoomStateUpdate: A room status update callback that notifies the SDK when the room connection status changes (such as room disconnection, login authentication failure, etc.) after logging in to the room.

  • OnRoomUserUpdate: User status update callback that notifies the SDK when a user is added or deleted from the room after logging in.

  • OnRoomStreamUpdate: Stream status update callback that notifys the SDK when a user pushes or deletes a new audio or video stream in the room after logging in.

Plain Text

engine.setEventHandler(new IZegoEventHandler() {

/** The following are common room-related callbacks */

/** Room status update callback */

@Override

public void onRoomStateUpdate(String roomID, ZegoRoomState state, int errorCode, JSONObject extendedData) {

/** Implement event callbacks as needed

}

/* user status update callback */

@Override

public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList userList) {

/** Implement event callbacks as needed

}

/** Stream status update callback */

@Override

public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList streamList, JSONObject extendedData){

/** Implement event callbacks as needed

}

});

3, push flow

1) Start pushing

Call the startPublishingStream interface, passing in the streamID parameter “streamID”, and send the local audio and video stream to the remote user.

Note that:

In the same AppID, ensure that streamID is globally unique. If different users in the same AppID push a stream with the same “streamID”, the user fails to push the stream.

Plain Text

/** Start to push flow */

engine.startPublishingStream(“stream1”);

2) Enable local Preview (optional)

If you want to see the local screen, you can call the startPreview interface to set the preview view and launch the local preview.

Plain Text

/ * *

* Launch preview, set the local preview View, use the SDK default View mode, and fill the entire View by scaling

* the following preview_view for UI interface SurfaceView/TextureView/SurfaceTexture object

* /

engine.startPreview(new ZegoCanvas(preview_view));

3) Listener event callback (optional)

According to the actual application needs, after the push stream, listen for notifications of events that you want to pay attention to, such as push stream status updates.

OnPublisherStateUpdate: callback for the push flow status update. After the push flow interface is successfully invoked, the SDK notifies the push flow status when the push flow status changes, for example, the push flow is abnormal due to network interruption.

Plain Text

engine.setEventHandler(new IZegoEventHandler() {

/** Common push-stream related callbacks */

/** Push flow status update callback */

@Override

public void onPublisherStateUpdate(String streamID, ZegoPublisherState state, int errorCode, JSONObject extendedData){

/** Implement event callbacks as needed

}

});

4, pull flow

1) Start pulling

Call the startPlayingStream interface and pull the remote audio and video stream based on the passed streamID parameter “streamID”.

The “streamID” pushed by the remote user is available from the onRoomStreamUpdate callback in the IZegoEventHandler agent.

Supports the following types of controls for streaming: SurfaceView, TextureView, and SurfaceTexture.

Plain Text

/ * *

* Start pull stream and set the remote pull stream rendering View. The View mode is the DEFAULT SDK mode, and the entire View is filled by scaling

* the following play_view for UI interface SurfaceView/TextureView/SurfaceTexture object

* /

engine.startPlayingStream(“stream1”, new ZegoCanvas(play_view));

2) Listener event callback (optional)

According to the actual application, listen for notifications of events that you want to pay attention to after pulling the stream, such as pulling the stream status update.

OnPlayerStateUpdate: Callback for stream status update. After the stream interface is successfully invoked, the SDK notifies the stream status when the stream status changes, for example, when the stream is pushed abnormally due to network interruption.

Plain Text

engine.setEventHandler(new IZegoEventHandler() {

/** Common pull-related callbacks */

/** Pull the flow state related callback */

@Override

public void onPlayerStateUpdate(String streamID, ZegoPlayerState state, int errorCode, JSONObject extendedData){

/** Implement event callbacks as needed

}

});

5. Stop the push and pull flow

1) Stop the push stream preview

Call the stopPublishingStream interface to stop sending the local audio and video streams to the remote user.

Plain Text

/** Stop pushing flow */

engine.stopPublishingStream();

If local preview is enabled, call the stopPreview interface to stopPreview.

Plain Text

/** Stop the local preview */

engine.stopPreview();

2) Stop the pull flow

Call stopPlayingStream interface to stop pulling remote pushed audio and video streams.

Plain Text

/** Stop pull flow */

engine.stopPlayingStream(streamID);

6. Exit the room

Call the logoutRoom interface to exit the room.

Plain Text

/** Exit the room */

engine.logoutRoom(“room1”);

7. Destroy engine

Call destroyEngine to destroy the engine to release the resources used by the SDK.

If necessary, you can pass in the “callback” parameter when destroying the engine and listen for the callback to ensure that the device hardware resources are released. This callback is only used to send notifications, and the developer cannot release engine-related resources within the callback. If the developer does not need to listen for callbacks, pass “null” to “callback”.

Plain Text

/** Destroy SDK */

ZegoExpressEngine.destroyEngine(null);

Above is the whole process of Java live broadcast, after watching you may wish to create a new project under the actual operation, any questions can also be private message or comments and I discuss ~~~~