This document describes how to quickly run GME Unreal Engine sample project and integrate the sample code to a project.
You need to activate the voice chat and voice messaging services of GME and get the AppId
and Key
in advance. For more information on how to apply for GME services, see Activating Services. appId
is the AppID
and authKey
is the permission key in the console.
Download the Unreal Engine sample project as instructed in SDK Download Guide. As the demo configurations for UE5 and UE4 are different, you need to download the sample project for the corresponding engine version.
After downloading, open the project directory, find UserConfig.cpp
in the Source\UEDemo1
path, and change the appID
and appKey
in the red box as shown below to the AppID
and permission key applied for in Service Management > Application Settings in the GME console.
1. Run the program
Click in the Editor to run the program.
2. Initialize
openID
, which is the unique identifier of a user in the application. The openID
value must be unique on each terminal.Click Login to initialize, and then click Voice Chat to enter the voice chat room configuration page.
3. Enter a voice chat room
After configuring the voice chat room ID, click JoinRoom to enter the room.
4. Use voice chat
The page will display the RoomID
for room entry and the local openID
.
After the mic and speaker are selected locally, repeat the above steps on another device to enter the same room and turn on the mic and speaker, so that communication can be implemented.
If 3D Voice Effect
is selected on both terminals, use the A, S, D, and W keys to move around and experience the directional 3D stereo effect.
5. Use voice messaging
Press and hold Push to Talk and speak into the mic. After you release the button, your voice message will be converted into text and displayed in the UI.
The main process to use GME voice chat is Init > EnterRoom > EnableMic > EnableSpeaker
. The main code of the sample project is in BaseViewController.cpp
and ExperientialDemoViewController.cpp
.
The initialization code is in the InitGME
function in the BaseViewController.cpp
file. It includes initialization, authentication initialization for voice message, and TMGDelegate
callback settings.
int UBaseViewController::InitGME(std::string sdkAppId, std::string sdkAppKey, std::string userId) {
int nAppid = atoi(sdkAppId.c_str());
int ret = ITMGContextGetInstance()->Init(sdkAppId.c_str(), userId.c_str());
ITMGContextGetInstance()->SetTMGDelegate(this);
int RetCode = (int) ITMGContextGetInstance()->CheckMicPermission();
FString msg = FString::Printf(TEXT("check Permission retcode =%d"), RetCode);
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, *msg);
char strSig[128] = {0};
unsigned int nLength = 128;
nLength = QAVSDK_AuthBuffer_GenAuthBuffer(nAppid, "0", userId.c_str(), sdkAppKey.c_str(), (unsigned char *)strSig, nLength);
ITMGContextGetInstance()->GetPTT()->ApplyPTTAuthbuffer(strSig, nLength);
m_appId = sdkAppId;
m_appKey = sdkAppKey;
m_userId = userId;
m_isEnableTips = false;
m_tipsMark = 0;
return ret;
}
Using GME requires periodic calls to the Poll
function in Tick
in the UEDemoLevelScriptActor.cpp
script.
void AUEDemoLevelScriptActor::Tick(float DeltaSeconds) {
Super::Tick(DeltaSeconds);
m_pTestDemoViewController->UpdateTips();
m_pCurrentViewController->UpdatePosition();
ITMGContextGetInstance()->Poll();
}
The room entry code is in the EnterRoom
function in the BaseViewController.cpp
file.
void UBaseViewController::EnterRoom(std::string roomID, ITMG_ROOM_TYPE roomType) {
int nAppid = atoi(m_appId.c_str());
UserConfig::SetRoomID(roomID);
char strSig[128] = {0};
unsigned int nLength = 128;
nLength = QAVSDK_AuthBuffer_GenAuthBuffer(nAppid, roomID.c_str(), m_userId.c_str(), m_appKey.c_str(), (unsigned char *)strSig, nLength);
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, TEXT("onEnterRoom"));
ITMGContextGetInstance()->EnterRoom(roomID.c_str(), roomType, strSig, nLength);
}
The room entry callback is in the OnEvent
function in the same script.
if (eventType == ITMG_MAIN_EVENT_TYPE_ENTER_ROOM) {
int32 result = JsonObject->GetIntegerField(TEXT("result"));
FString error_info = JsonObject->GetStringField(TEXT("error_info"));
if (result == 0) {
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 20.0f, FColor::Yellow, TEXT("Enter room success."));
}
else {
FString msg = FString::Printf(TEXT("Enter room failed. result=%d, info = %ls"), result, *error_info);
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 20.0f, FColor::Yellow, *msg);
}
onEnterRoomCompleted(result, error_info);
Device enablement code after successful room entry is in ExperientialDemoViewController.cpp
.
void UExperientialDemoViewController::onCheckMic(bool isChecked) {
//GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, L"onCheckMic");
ITMGContext *pContext = ITMGContextGetInstance();
if (pContext) {
ITMGAudioCtrl *pTmgCtrl = pContext->GetAudioCtrl();
if (pTmgCtrl) {
pTmgCtrl->EnableMic(isChecked);
}
}
}
void UExperientialDemoViewController::onCheckSpeaker(bool isChecked) {
//GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, L"onCheckSpeaker");
ITMGContext *pContext = ITMGContextGetInstance();
if (pContext) {
ITMGAudioCtrl *pTmgCtrl = pContext->GetAudioCtrl();
if (pTmgCtrl) {
pTmgCtrl->EnableSpeaker(isChecked);
}
}
}
For the connection of 3D sound effect, see 3D Sound Effect. In the project, initialize the 3D sound effect feature first with the code in ExperientialDemoViewController.cpp
.
void UExperientialDemoViewController::onCheckSpatializer(bool isChecked) {
char buffer[256]={0};
// snprintf(buffer, sizeof(buffer), "%s3d_model", getFilePath().c_str());
snprintf(buffer, sizeof(buffer), "%sgme_2.8_3d_model.dat", getFilePath().c_str());
int ret1 = ITMGContextGetInstance()->GetAudioCtrl()->InitSpatializer(buffer);
int ret2 = ITMGContextGetInstance()->GetAudioCtrl()->EnableSpatializer(isChecked, false);
FString msg = FString::Printf(TEXT("InitSpatializer=%d, EnableSpatializer ret=%d"), ret1, ret2);
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, msg);
}
Call the UpdatePosition
function in Tick
in the UEDemoLevelScriptActor.cpp
script .
void AUEDemoLevelScriptActor::Tick(float DeltaSeconds) {
Super::Tick(DeltaSeconds);
m_pTestDemoViewController->UpdateTips();
m_pCurrentViewController->UpdatePosition();
ITMGContextGetInstance()->Poll();
}
void UBaseViewController::UpdatePosition() {
if (!m_isCreated)
return;
ITMGRoom *pTmgRoom = ITMGContextGetInstance()->GetRoom();
if (!pTmgRoom)
{
return;
}
int nRange = GetRange();
pTmgRoom->UpdateAudioRecvRange(nRange);
FVector cameraLocation = UGameplayStatics::GetPlayerCameraManager(m_pActor->GetWorld(), 0)->GetCameraLocation();
FRotator cameraRotation = UGameplayStatics::GetPlayerCameraManager(m_pActor->GetWorld(), 0)->GetCameraRotation();
FString msg = FString::Printf(TEXT("location(x=%.2f,y=%.2f,z=%.2f), rotation(pitch=%.2f,yaw=%.2f,roll=%.2f)"),
cameraLocation.X, cameraLocation.Y, cameraLocation.Z, cameraRotation.Pitch, cameraRotation.Yaw, cameraRotation.Roll);
int position[] = { (int)cameraLocation.X,(int)cameraLocation.Y, (int)cameraLocation.Z };
FMatrix matrix = ((FRotationMatrix)cameraRotation);
float forward[] = { matrix.GetColumn(0).X,matrix.GetColumn(1).X,matrix.GetColumn(2).X };
float right[] = { matrix.GetColumn(0).Y,matrix.GetColumn(1).Y,matrix.GetColumn(2).Y };
float up[] = { matrix.GetColumn(0).Z,matrix.GetColumn(1).Z,matrix.GetColumn(2).Z };
pTmgRoom->UpdateSelfPosition(position, forward, right, up);
SetPositionInfo(msg);
}
Enable 3D effects in ExperientialDemoViewController.cpp
.
void UExperientialDemoViewController::onCheckSpatializer(bool isChecked) {
char buffer[256]={0};
// snprintf(buffer, sizeof(buffer), "%s3d_model", getFilePath().c_str());
snprintf(buffer, sizeof(buffer), "%sgme_2.8_3d_model.dat", getFilePath().c_str());
int ret1 = ITMGContextGetInstance()->GetAudioCtrl()->InitSpatializer(buffer);
int ret2 = ITMGContextGetInstance()->GetAudioCtrl()->EnableSpatializer(isChecked, false);
FString msg = FString::Printf(TEXT("InitSpatializer=%d, EnableSpatializer ret=%d"), ret1, ret2);
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.0f, FColor::Yellow, msg);
}
Was this page helpful?