Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Build a Java‑Based Android E‑Commerce Live Streaming App with Zego SDK
Mobile Development 10 min read

Build a Java‑Based Android E‑Commerce Live Streaming App with Zego SDK

This guide walks through creating an Android e‑commerce live‑streaming app using Java and the Zego real‑time audio‑video SDK, covering SDK integration, room creation, token generation, stream publishing and playback, and essential security considerations, with code snippets and practical implementation steps.

Introduction

Nowadays many internet apps include e‑commerce live streaming, and developers need a quick way to build such features on Android. This article demonstrates how to implement a basic live‑streaming demo using Java and the Zego real‑time audio‑video SDK.

1. Implement E‑Commerce Live Streaming with Java

1.1 Integrate Zego Live SDK

Refer to the official documentation:

https://doc-zh.zego.im/article/195?source=juejin&article16

. The integration details are omitted for brevity.

1.2 Initialize Audio‑Video SDK Engine

All Zego SDK calls are encapsulated in a singleton class. The constructor initializes the engine:

private Zego(Application app) {
    ZegoEngineProfile profile = new ZegoEngineProfile();
    profile.appID = KeyCenter.APPID;
    profile.scenario = ZegoScenario.GENERAL; // General scenario
    profile.application = app;
    mEngine = ZegoExpressEngine.createEngine(profile, null);
}

The required AppID can be obtained from the Zego console: https://console.zego.im?source=juejin&article16.

2. Create Live Room and Login

Login UI example:

2.1 Host Creates Room

2.1.1 Validate Room ID

Before creating a room, verify that the chosen room ID is not already in use. If you do not have a private server, you can call Zego’s server‑side API:

https://doc-zh.zego.im/article/8780?source=juejin&article16

to query current room participants.

In short, the server‑side API returns a JSON response with the room’s current status.

2.1.2 Create and Join Room

The login function is encapsulated in the Zego class:

public boolean loginRoom(String userId, String userName, String roomId, String token) {
    ZegoUser user = new ZegoUser(userId, userName);
    ZegoRoomConfig config = new ZegoRoomConfig();
    config.token = token; // obtained from backend
    config.isUserStatusNotify = true;
    mEngine.loginRoom(roomId, user, config);
    return true;
}

The token is generated on a backend server using symmetric encryption; keep the secret key private.

2.2 Audience Login

Audience devices use the same loginRoom method to enter the room.

3. Publishing and Playing Streams

3.1 Host Publishing

Request camera and microphone permissions. Start camera preview. Publish the stream to Zego’s server for distribution.

3.1.1 Start Camera Preview

Code to start preview:

ZegoCanvas canvas = new ZegoCanvas(textureView);
canvas.viewMode = ASPECT_FILL;
mEngine.startPreview(canvas);

3.1.2 Publish and Stop Publishing

mEngine.startPublishingStream(streamId);
// ... when finished
mEngine.stopPublishingStream();

3.2 Audience Playback

Playing a remote stream requires only the TextureView and stream ID:

ZegoCanvas canvas = new ZegoCanvas(textureView);
canvas.viewMode = ASPECT_FILL;
mEngine.startPlayingStream(streamId, canvas);

3.3 Unified Preview Wrapper

Define a simple data class:

public class PreviewItem {
    public TextureView tv;
    public String streamId;
    public PreviewItem(TextureView tv, String streamId) {
        this.tv = tv;
        this.streamId = streamId;
    }
}

Play preview method that works for both host and audience:

public void playPreview(PreviewItem pi, boolean isMyself) {
    ZegoCanvas canvas = new ZegoCanvas(pi.tv);
    canvas.viewMode = ASPECT_FILL;
    mEngine.stopPublishingStream();
    if (isMyself) {
        mEngine.startPublishingStream(pi.streamId);
        mEngine.startPreview(canvas);
    } else {
        mEngine.startPlayingStream(pi.streamId, canvas);
    }
}

4. Additional Considerations

Security: generate tokens on a backend server to avoid exposing the secret key in the client.

4.1 How Audience Learns Host Stream ID

The host’s stream ID can be obtained via the onRoomStreamUpdate callback. It is recommended to use a naming convention such as RoomID_UserID to avoid collisions.

4.2 Obtaining Host ID, Product Info, and Room Name

The host can broadcast custom messages after users join the room using sendCustomCommand:

public void sendMsg(String roomId, ArrayList<ZegoUser> userList, Msg msg) {
    String msgPack = msg.toString();
    mEngine.sendCustomCommand(roomId, msgPack, userList, new IZegoIMSendCustomCommandCallback() {
        @Override
        public void onIMSendCustomCommandResult(int errorCode) {}
    });
}

5. Source Code

The complete Android demo source code is available at:

https://github.com/KaleTom/E-commerceLive?source=juejin&article16

.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

JavaMobile Developmentlive streamingAndroidZego SDK

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion