tencent cloud

Feedback

Integrating TUIRoom (Android)

Last updated: 2023-10-13 11:28:35

    Overview

    TUIRoom is an open-source UI component for audio/video communication. With just a few lines of code changes, you can integrate it into your project to implement screen sharing, beauty filters, low-latency video calls, and other features. In addition to the Android component, we also offer components for iOS, Windows, macOS, and more.
    Note
    All components of TUIKit use Tencent Cloud's TRTC and Chat services. When you activate TRTC, Chat and the trial edition of the Chat SDK (which supports up to 100 DAUs) will also be activated automatically. For Chat billing details, see Pricing.
    
    
    
    

    Integration

    Step 1. Download and import the TUIRoom component

    Go to the component's GitHub page, clone or download the code, and copy the tuiroom and debug, and tuibeauty folders in the Android directory to your project. Then, do the following to import the component:
    1. Add the code below in setting.gradle:
    include ':tuiroom'
    include ':debug'
    include ':tuibeauty'
    2. Add dependencies on tuiroom, debug, and tuibeauty to the build.gradle file in app:
    api project(':tuiroom')
    api project(':debug')
    api project(':tuibeauty')
    3. Add the TRTC SDK (liteavSdk) and Chat SDK (imsdk) dependencies in build.gradle in the root directory:
    ext {
    liteavSdk = "com.tencent.liteav:LiteAVSDK_TRTC:latest.release"
    imSdk = "com.tencent.imsdk:imsdk-plus:latest.release"
    }

    Step 2. Configure permission requests and obfuscation rules

    1. Configure permission requests for your app in AndroidManifest.xml. The SDKs need the following permissions (on Android 6.0 and later, the mic permission must be requested at runtime.)
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus" />
    2. In the proguard-rules.pro file, add the SDK classes to the "do not obfuscate" list.
    -keep class com.tencent.** { *;}

    Step 3. Create and initialize an instance of the component

    // 1. Log in to the component
    TUILogin.addLoginListener(new TUILoginListener() {
    @Override
    public void onKickedOffline() { // Callback for forced logout (for example, due to login from another device)
    }
    
    @Override
    public void onUserSigExpired() { // Callback for `userSig` expiration
    }
    });
    
    TUILogin.login(context, "Your SDKAppId", "Your userId", "Your userSig", null);
    
    
    // 2. Initialize the `TUIRoom` instance
    TUIRoom tuiRoom = TUIRoom.sharedInstance(this);

    Parameter description

    SDKAppID: TRTC application ID. If you haven't activated TRTC, log in to the TRTC console, create a TRTC application, click Application Info, and select the Quick Start tab to view its SDKAppID.
    
    
    Secretkey: TRTC application key. Each secret key corresponds to a SDKAppID. You can view your application’s secret key on the Application Management page of the TRTC console.
    userId: ID of the current user, which is a string that can contain only letters (a-z and A-Z), digits (0-9), hyphens (-), and underscores (_). We recommend that you keep it consistent with your user account system.
    UserSig: Security signature calculated based on SDKAppID, userId, and Secretkey. You can click here to quickly generate a UserSig for testing. For more information, see UserSig.

    Step 4. Implement group audio/video communication

    1. Create a room
    tuiRoom.createRoom(12345, TUIRoomCoreDef.SpeechMode.FREE_SPEECH, true, true);
    2. Join a room
    tuiRoom.enterRoom(12345, true, true);

    Step 5. Implement room management (optional)

    1. The room owner calls TUIRoomCore#destroyRoom to close the room.
    // 1. The room owner calls the API below to close the room.
    mTUIRoomCore.destroyRoom(new TUIRoomCoreCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    
    }
    });
    
    Other users in the room will receive the `onDestroyRoom` callback.
    @Override
    public void onDestroyRoom() {
    // The room owner closes and exits the room.
    }
    2. A user in the room calls TUIRoomCore#leaveRoom to leave the room.
    // 1. A user (not the room owner) calls the API below to leave the room.
    mTUIRoomCore.leaveRoom(new TUIRoomCoreCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    
    }
    });
    
    Other users in the room will receive the `onRemoteUserLeave` callback.
    @Override
    public void onRemoteUserLeave(String userId) {
    Log.d(TAG, "onRemoteUserLeave userId: " + userId);
    }

    Step 6. Implement screen sharing (optional)

    Call TUIRoomCore#startScreenCapture to implement screen sharing.
    // 1. Add the SDK’s screen sharing activity and permission in `AndroidManifest.xml`
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <application>
    <activity
    android:name="com.tencent.rtmp.video.TXScreenCapture$TXScreenCaptureAssistantActivity"
    android:theme="@android:style/Theme.Translucent" />
    </application>
    
    // 2. Request the floating window permission in your UI
    if (Build.VERSION.SDK_INT >= 23) {
    if (!Settings.canDrawOverlays(getApplicationContext())) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 100);
    } else {
    startScreenCapture();
    }
    } else {
    startScreenCapture();
    }
    
    // 3. System callback result
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
    if (Build.VERSION.SDK_INT >= 23) {
    if (Settings.canDrawOverlays(this)) {
    // The user grants the permission.
    startScreenCapture();
    } else {
    }
    }
    }
    }
    
    // 4. Start screen sharing
    private void startScreenCapture() {
    TRTCCloudDef.TRTCVideoEncParam encParams = new TRTCCloudDef.TRTCVideoEncParam();
    encParams.videoResolution = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_1280_720;
    encParams.videoResolutionMode = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_MODE_PORTRAIT;
    encParams.videoFps = 10;
    encParams.enableAdjustRes = false;
    encParams.videoBitrate = 1200;
    
    TRTCCloudDef.TRTCScreenShareParams params = new TRTCCloudDef.TRTCScreenShareParams();
    mTUIRoom.stopCameraPreview();
    mTUIRoom.startScreenCapture(encParams, params);
    }

    Suggestions and Feedback

    If you have any suggestions or feedback, please contact colleenyu@tencent.com.
    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