tencent cloud

Feedback

Integrating TUIVoiceRoom (Android)

Last updated: 2024-01-18 11:18:54

    Overview

    TUIVoiceRoom is an open-source audio/video UI component. After integrating it into your project, you can make your application support the group audio chat scenario simply by writing a few lines of code. It also supports the iOS platform. Its basic features are as shown below:
    Note
    All TUIKit components are based on two PaaS services of Tencent Cloud, namely TRTC and Chat. When you activate TRTC, the Chat SDK trial edition (which supports up to 100 DAUs) will be activated automatically. For billing details of Chat, see Pricing.
    
    
    
    

    Integration

    Step 1. Download and import the TUIVoiceRoom component

    Go to GitHub, clone or download the code, copy the Android/Source directory to your project, and complete the following import operations:
    Add the code below in setting.gradle:
    include ':Source'
    Add dependencies on Source to the build.gradle file in app:
    api project(':Source')
    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

    Configure permission requests for your app in AndroidManifest.xml. The SDKs need the following permissions (on Android 6.0 and later, the mic access 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.RECORD_AUDIO" />
    In the proguard-rules.pro file, add the SDK classes to the "do not obfuscate" list.
    -keep class com.tencent.** { *;}

    Step 3. Initialize and log in to the component

    // 1. Initialize
    TRTCVoiceRoom mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(this);
    mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
    );
    // 2. Log in
    mTRTCVoiceRoom.login(SDKAppID, userId, userSig, new TRTCVoiceRoomCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    if (code == 0) {
    // Logged in
    }
    }
    });
    Parameter description:
    SDKAppID: The 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: The 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: The 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: The security protection signature calculated based on SDKAppID, userId, and Secretkey. You can click here to directly generate a debugging userSig online. For more information, see UserSig.

    Step 4. Implement the audio chat room

    1. The room owner creates an audio chat room through TRTCVoiceRoom#createRoom.
    // 1. The room owner calls an API to create a room
    int roomId = 12345; // Room ID
    final TRTCVoiceRoomDef.RoomParam roomParam = new TRTCVoiceRoomDef.RoomParam();
    roomParam.roomName = "Room name";
    roomParam.needRequest = false; // Whether the room owner’s permission is required for listeners to speak
    roomParam.seatCount = 7; // Number of room seats. In this example, the number is 7. 6 seats are available after you take one.
    roomParam.coverUrl = "URL of room cover image";
    mTRTCVoiceRoom.createRoom(roomId, roomParam, new TRTCVoiceRoomCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    if (code == 0) {
    // Room created successfully
    }
    }
    });
    2. A listener enters the audio chat room through TRTCVoiceRoom#enterRoom.
    // 1. A listener calls an API to enter the room
    mTRTCVoiceRoom.enterRoom(roomId, new TRTCVoiceRoomCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    if (code == 0) {
    // Entered room successfully
    }
    }
    });
    3. A listener mics on through TRTCVoiceRoom#enterSeat.
    // 1. A listener calls an API to mic on
    int seatIndex = 2; // Seat index
    mTRTCVoiceRoom.enterSeat(seatIndex, new TRTCVoiceRoomCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    if (code == 0) {
    // Operation succeeded
    }
    }
    });
    
    // 2. The `onSeatListChange` callback is received, and the seat list is refreshed
    @Override
    public void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {
    }
    4. The room owner makes a listener a speaker through TRTCVoiceRoom#pickSeat.
    // 1. The room owner makes a listener a speaker
    int seatIndex = 2; // Seat index
    String userId = "123"; // The ID of the user who will become a speaker
    mTRTCVoiceRoom.pickSeat(1, userId, new TRTCVoiceRoomCallback.ActionCallback() {
    @Override
    public void onCallback(int code, String msg) {
    if (code == 0) {
    // Operation succeeded
    }
    }
    });
    
    // 2. The `onSeatListChange` callback is received, and the seat list is refreshed
    @Override
    public void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {
    }
    5. A listener requests to speak through TRTCVoiceRoom#sendInvitation.
    // Listener
    // 1. A listener calls an API to request to speak
    String seatIndex = "1"; // Seat index
    String userId = "123"; // User ID
    String inviteId = mTRTCVoiceRoom.sendInvitation("takeSeat", userId, seatIndex, null);
    
    // 2. Place the user in the seat after the invitation is accepted
    @Override
    public void onInviteeAccepted(String id, String invitee) {
    if(id.equals(inviteId)) {
    mTRTCVoiceRoom.enterSeat(index, null);
    }
    }
    
    // Room owner
    // 1. The room owner receives the request
    @Override
    public void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {
    if (cmd.equals("takeSeat")) {
    // 2. The room owner accepts the request
    mTRTCVoiceRoom.acceptInvitation(id, null);
    }
    }
    6. The room owner invites a listener to speak through TRTCVoiceRoom#sendInvitation.
    // Room owner
    // 1. The room owner calls an API to invite a listener to speak
    String seatIndex = "1"; // Seat index
    String userId = "123"; // User ID
    String inviteId = mTRTCVoiceRoom.sendInvitation("pickSeat", userId, seatIndex, null);
    
    // 2. Place the user in the seat after the invitation is accepted
    @Override
    public void onInviteeAccepted(String id, String invitee) {
    if(id.equals(inviteId)) {
    mTRTCVoiceRoom.pickSeat(index, null);
    }
    }
    
    // Listener
    // 1. The listener receives the invitation
    @Override
    public void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {
    if (cmd.equals("pickSeat")) {
    // 2. The listener accepts the invitation
    mTRTCVoiceRoom.acceptInvitation(id, null);
    }
    }
    7. Implement text chat through TRTCVoiceRoom#sendRoomTextMsg.
    // Sender: Sends a text chat message
    mTRTCVoiceRoom.sendRoomTextMsg("Hello World!", null);
    // Receiver: Listens for text chat messages
    mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
    @Override
    public void onRecvRoomTextMsg(String message, TRTCVoiceRoomDef.UserInfo userInfo) {
    Log.d(TAG, "Received a message from" + userInfo.userName + ": " + message);
    }
    });
    8. Implement on-screen commenting through TRTCVoiceRoom#sendRoomCustomMsg.
    // A sender can customize CMD to distinguish on-screen comments and likes.
    // For example, use "CMD_DANMU" to indicate on-screen comments and "CMD_LIKE" to indicate likes.
    mTRTCVoiceRoom.sendRoomCustomMsg("CMD_DANMU", "Hello world", null);
    mTRTCVoiceRoom.sendRoomCustomMsg("CMD_LIKE", "", null);
    // Receiver: Listens for custom messages
    mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
    @Override
    public void onRecvRoomCustomMsg(String cmd, String message, TRTCVoiceRoomDef.UserInfo userInfo) {
    if ("CMD_DANMU".equals(cmd)) {
    // An on-screen comment is received
    Log.d(TAG, "Received an on-screen comment from" + userInfo.userName + ": " + message);
    } else if ("CMD_LIKE".equals(cmd)) {
    // A like is received
    Log.d(TAG, userInfo.userName + "liked you.");
    }
    }
    });

    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