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.
Note:
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.
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.
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);
}
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();
}
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);
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);
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();
}