In-game Voice Chat

Function Realization Process

Update Time:2020-11-16 11:22

One of the typical game voice scenarios is that during the game, members in the same room conduct game voice conversations.

Take the game voice between 2 people as an example, the main process is as follows:

Note:

  1. In the above process, the game voice between 2 room members is taken as an example. In fact, ZEGO SDK supports multiplayer game voice. It is recommended that developers design according to their needs.

  2. In order to facilitate developers to understand the logic in AudioLive more quickly, the following sections will pick out and explain the core source code fragments. Developers can also directly read AudioLive source code, the two are the same.

1 Login Room

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

The real-time audio of the relevant source code snippets registered in AudioLive is shown below, for reference only:

SessionActivity.java

// log in to the room, it will automatically pull the stream after logging in successfully
private void login(String roomId) {
    tvEventTips.setText(getString(R.string.zg_tip_begin_login_room, roomId));
    zegoAudioRoom.setUserStateUpdate(true);
    boolean success = zegoAudioRoom.loginRoom(roomId, new ZegoLoginAudioRoomCallback() {
        @Override
        public void onLoginCompletion(int state) {
            MainActivity.ZGLog.d("onLoginCompletion: 0x%1$x", state);

            if (state == 0) {
                hasLogin = true;

                btnAux.setEnabled(true);
                btnMute.setEnabled(true);
                btnRecorder.setEnabled(true);
                btnMicrophone.setEnabled(true);

                if (PrefUtils.isManualPublish()) {
                    btnCommunicate.setEnabled(true);
                }

                tvEventTips.setText(R.string.zg_tip_login_success);
            } else {
                Toast.makeText(SessionActivity.this, String.format("Login Error: 0x%1$x", state), Toast.LENGTH_LONG).show();
                tvEventTips.setText(getString(R.string.zg_tip_login_failed_state, state));
            }
        }
    });
    MainActivity.ZGLog.d("login: %s", success);
    if (!success) {
        tvEventTips.setText(R.string.zg_tip_login_failed);
    }
}

2 Publish Live Broadcast

Members in the room can post live broadcast, but there are two ways to post live broadcast:

Automatically publish live broadcast, please refer to: Quick Start-Automatically Publish Live Broadcast. To publish the live broadcast manually, please refer to: Quick Start-Manually Publish the Live Broadcast.

Note: If the user chooses to publish the live broadcast automatically, after logging in successfully, the SDK will automatically start streaming, and the user does not need to manually trigger.

The following is a demonstration of manually releasing live-related source code snippets in AudioLive, for reference only:

SessionActivity.java

// the user actively clicks the publish live button
private void handleCommunicate() {
    if (!PrefUtils.isManualPublish()) return;

    if (hasPublish) {
        zegoAudioRoom.stopPublish();
        btnCommunicate.setText(R.string.zg_start_communicate);
        ZegoAudioStream myStream = new ZegoAudioStream(publishStreamId, PrefUtils.getUserId(), PrefUtils.getUserName());
        streamAdapter.removeItem(myStream);
        publishStreamId = null;
        hasPublish = false;
        tvEventTips.setText(R.string.zg_tip_stop_publish);
    } else {
        btnCommunicate.setEnabled(false);
        zegoAudioRoom.startPublish();
    }
}

3 Play Live Broadcast

Members in the room can play the live broadcast. After logging in to the room, the SDK will automatically pull the stream to play the live broadcast. Users do not need to manually trigger the pull stream.

If the developer wants to know more about broadcasting live broadcast, please refer to: Quick Start-Broadcast Live Broadcast.

4 End Audio Live Broadcast

The operations after the audio call is over are mainly to log out of the room, clean up the view or data, etc. Developers can call on demand.

The source code snippets related to the ending audio live broadcast in AudioLive are shown below for reference only:

SessionActivity.java

// when the user exits the SessionActivity interface
@Override
public void onBackPressed() {
    if (hasLogin) {
        logout();
    }
    removeCallbacks();
    super.onBackPressed();
}

// exit the room
private void logout() {
    boolean success = zegoAudioRoom.logoutRoom();
    streamAdapter.clear();
    hasLogin = false;
    hasPublish = false;

    btnAux.setEnabled(false);
    btnMute.setEnabled(false);
    btnRecorder.setEnabled(false);
    btnMicrophone.setEnabled(false);

    btnCommunicate.setText(R.string.zg_start_communicate);
    if (PrefUtils.isManualPublish()) {
        btnCommunicate.setEnabled(false);
    }

    MainActivity.ZGLog.d("logout: %s", success);
}

// remove all callbacks
private void removeCallbacks() {
    zegoAudioRoom.setAudioRoomDelegate(null);
    zegoAudioRoom.setAudioPublisherDelegate(null);
    zegoAudioRoom.setAudioPlayerDelegate(null);
    zegoAudioRoom.setAudioLiveEventDelegate(null);
    zegoAudioRoom.setAudioRecordDelegate(null);
    zegoAudioRoom.setAudioDeviceEventDelegate(null);
    zegoAudioRoom.setAudioPrepareDelegate(null);
    zegoAudioRoom.setAudioAVEngineDelegate(null);
}