tencent cloud

Feedback

Quick Unreal Engine Demo Run

Last updated: 2022-06-08 15:54:51

    This document describes how to quickly run GME Unreal Engine demo and connect the sample code to a project.

    Running Unreal Engine Demo

    Environment requirements

    • Unreal Engine 4.22 or higher.
    • Microsoft Visual Studio.
    • A configuration environment that can run Unreal Engine projects.

    Prerequisites

    You have registered a Tencent Cloud account and completed identity verification.

    1. Apply for the GME service

    Apply for the GME voice chat service and get the voice chat Appid and Key.

    2. Download the project

    Download the Unreal Engine demo through the Download Guide.

    3. Configure the project

    After downloading, open the project directory, find UserConfig.cpp in the Source\UEDemo1 path, and change the appID and appKey in the blue box as shown below to the GME voice chat service's Appid and key obtained in step 1.

    4. Compile and run

    1. Run the program

    Click in the Editor to enter the demo and run the program.

    2. Initialize

    • UserId: equivalent to openid, which must be unique on each terminal.
    • Voice Chat: voice chat feature UI.
    • Voice Message: voice message feature UI.

    Click Login to initialize, and then click Voice Chat to enter the voice chat room configuration page.

    3. Enter the voice chat room

    • RoomId: room ID. Users in the same room can communicate with each other by voice.
    • RoomType: use Fluency to enter the room.
    • JoinRoom: enter the voice room.
    • Back: go back to the previous page.

    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.

    • Mic: select to turn on the mic.
    • Speaker: select to turn on the speaker.
    • 3D Voice Effect: select to enable 3D sound effects.
    • Voice Change: select to enable voice changing effects.

    After the mic and speaker are selected locally, repeat the above steps on another device to enter the same room and select 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 message

    • Language: select the target language for text conversion. For example, if you speak Chinese, choose Mandarin.
    • Audio: click to listen after recording.
    • Audio-to-Text: text content of the voice message.
    • Push To Talk: press and hold to record.
    • Back: go back to the previous page.

    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.

    Project sample code overview

    The main process to use GME voice chat is Init > EnterRoom > EnableMic > EnableSpeaker. The main code of the demo is in BaseViewController.cpp and ExperientialDemoViewController.cpp.

    Initialization

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

    Room entry

    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

    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);
    }
    }
    }
    

    3D sound effect

    For the connection of 3D sound effect, see 3D Sound Effect. In the demo, 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);
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support