Documentation
GameAudio In-Game Voice Chat
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • In-Game Voice Chat
  • Solution Implementation
  • Unity3D

Function Realization Process

Last updated:2022-03-22 13:06

1 Function Description

Game voice refers to the addition of voice functions to the game. After players join the game, they can communicate with other players in the designated public voice room. Make the game more lively and interesting, and enhance the user's gaming experience.

  • Players can quickly and steadily switch to other public voice rooms or team voices. In the corresponding room, only the voices of other players in the corresponding room can be heard.
  • Players can exit the voice room autonomously. After exiting the room, they can no longer hear the voice of other players in the corresponding room.
  • The player can freely switch the communication mode in the voice room, mute the microphone, turn on the microphone or free microphone mode, the player can turn off the voice of a certain other player to play on the local end (such as the other party's foul language, noisy voice), and the player can also When turning on other turned off, the player’s voice is played on the local end to achieve flexible control of the voice room communication.
  • Players can freely control the streaming volume (the volume of their own voice), the streaming volume (the playback volume of other players' voices at the local end) and the playback volume (speaker playback volume) during the game to achieve diversified volume control.
  • If the game exists, the spectator can be set as the spectator in the spectator mode, and only the voices of other players in the room can be heard.
  • Players can have instant chat (IM), and the corresponding room can only see the instant messages of the players in the corresponding room.

2 Process Description

  1. Initialize the SDK: After the SDK is initialized, login and push and pull streaming operations can be performed.
  2. Set up callbacks: set up callbacks for various events that need to be monitored, such as login, pull stream, push stream, member join, etc.
  3. Join the room: After the user selects the game room, enter the game room and log in to the corresponding voice room.
  4. Streaming: Play audio and video streams of other users.
  5. Push streaming: After the user opens the corresponding permissions, the local audio and video can be pushed out.
  6. Disable/enable microphone: the user can disable or enable the local microphone.
  7. Exit the room: The user needs to stop streaming, stop streaming, and then log out of the room.

Note:

  1. In the flowchart, the game voice between 2 room members is taken as an example. The actual SDK supports multiplayer game voice, and it is recommended that developers design according to their needs.

  2. In order to make it easier for developers to understand the logic of the game voice faster, the following mainly describes the realization of the core functions.

3 Code Implementation Instructions

3.1 Login Room

Call loginRoom to log in to the room.


        // Initialize sdk and return to monitor
        zego_client_.on_init_sdk_ = OnInitSdk;

        // Engine start monitoring
        zego_client_.on_engine_start_ = OnAVEngineStart;

        // Engine end monitoring
        zego_client_.on_engine_stop_ = OnAVEngineStop;

        // Login to monitor
        zego_client_.on_login_room_ = OnLoginRoom;

        // Log out to monitor
        zego_client_.on_logout_room_ = OnLogoutRoom;

        // Acquisition resolution change monitoring
        zego_client_.on_capture_video_size_changed_ = OnCapVideoSizeChanged;

        // Capture the first frame of video monitoring
        zego_client_.on_capture_video_first_frame_ = OnCaptureVideoFirstFrame;

        // Stream new or stream delete event listener
        zego_client_.on_stream_updated_ = OnStreamUpdated;

        // Push stream status update monitoring
        zego_client_.on_publish_state_update_ = OnPublishStateUpdate;

        // Push stream quality update monitoring
        zego_client_.on_publish_qulity_update_ = OnPublishQulityUpdate;

        // Pull stream status update monitoring
        zego_client_.on_play_state_update_ = OnPlayStateUpdate;

        // Pull stream quality update monitoring
        zego_client_.on_play_quality_update_ = OnPlayQualityUpdate;

        // Audio or video equipment (camera, microphone, speaker, etc.) error event monitoring
        zego_client_.on_device_error_ = OnDeviceError;

        // Disconnected event listener
        zego_client_.on_disconnect_ = OnDisconnect;

        // Monitoring of temporary network interruption events
        zego_client_.on_temp_broken_ = OnTempBroken;

        // Reconnected to the server event listener
        zego_client_.on_reconnected_ = OnReconnected;

        // Listen for the kicked-out event
        zego_client_.on_kickout_ = OnKickOut;

        if (enable_video_external_capture_clicked_)
        {
            zego_client_.on_external_video_capture_engine_start_ = OnExternalVideoCaptureEngineStart;

            zego_client_.on_external_video_capture_engine_stop_ = OnExternalVideoCaptureEngineStop;

            zego_client_.on_external_video_capture_capture_start_ = OnExternalVideoCaptureCaptureStart;

            zego_client_.on_external_video_capture_capture_stop_ = OnExternalVideoCaptureCaptureStop;

            zego_client_.on_external_video_capture_preview_start_ = OnExternalVideoCapturePreviewStart;

            zego_client_.on_external_video_capture_preview_stop_ = OnExternalVideoCapturePreviewStop;

            zego_client_.on_external_video_capture_traffic_control_ = OnExternalVideoCaptureTrafficControl;

            zego_external_video_capture_config config = new zego_external_video_capture_config();
            config.type = zego_external_video_capture_type.zego_external_video_capture_type_memory_data;

            // Before initializing the SDK, call to open the external collection function
            zego_client_.EnableExternalVideoCapture(config, true);

        }

// Log in to the room, it will automatically pull the stream after logging in successfully
    public void LoginRoom()
    {
        GameObject game_obj = GameObject.Find("roomid");

        InputField field = game_obj.GetComponent<InputField>();

        string room_id = field.text;

        string room_name = "test_zego_unity";

        zego_client_.LoginRoom(room_id, room_name, zego_room_role.zego_room_role_anchor);
    }

3.2 Push And Stop Pushing

Call startPublishing to push streaming. If you don’t need to continue to push, please call stopPublishing to stop streaming.

Note: streamID needs to be globally unique, and only supports numbers, underscores and letters up to 256 bytes in length.


// The user actively clicks the publish live button
    public void StartPublishing()
    {
        publish_stream_id_ = GetMD5(user_id_);

        ZGLog("Start to push stream, stream id = "+ publish_stream_id_);

        zego_client_.StartPublishing("test_title", publish_stream_id_, zego_publish_type.zego_publish_type_join, "test_param");
    }

    public void StopPublishing()
    {
        ZGLog("Stop Push Stream");
        zego_client_.StopPublishing();
    }

3.3 Pull Flow And Stop Pulling Flow

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 needs to be globally unique, and only supports numbers, underscores and letters up to 256 bytes in length.


  playing_stream_id_ = stream_info_list[0].stream_id;
  zego_client_.StartPlayingStream(playing_stream_id_, System.IntPtr.Zero, "test_param");

  playing_stream_id_ = stream_info_list[0].stream_id;
  zego_client_.StopPlayingStream(stream_info_list[0].stream_id);

3.4 Disable Enable Microphone

Call SetMicDeviceMute to disable or enable your own microphone.

   bool mute = true;
   zego_client_.SetMicDeviceMute(string device_id, mute);

   bool mute = false;
   zego_client_.SetMicDeviceMute(string device_id, mute);

3.5 Exit The Room

The operations after the audio call is over are mainly to log out of the room, clean up the view or data, etc. The developer can call them as needed.


  // Log out to monitor
  zego_client_.on_logout_room_ = OnLogoutRoom;

// Exit the room
public void LogoutRoom()
{
   video_capture_task_start_ = false;
   //Stop streaming
   zego_client_.StopPublishing();
   //Stop preview
   zego_client_.StopPreview();
   zego_client_.LogOutRoom();
}



Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code