tencent cloud

Feedback

Android, iOS, Windows, and macOS

Last updated: 2022-07-04 12:15:12

    This document describes how to enter a TRTC room. Only after entering an audio/video room can a user subscribe to the audio/video streams of other users in the room or publish his or her own audio/video streams.

    Call Guide

    Step 1. Import the SDK and set the application permissions

    Import the SDK as instructed in iOS.

    Step 2. Create an SDK instance and set an event listener

    Call the initialization API to create a TRTC object instance.

    // Create an SDK instance in singleton mode and set an event listener
    // Create trtc instance(singleton) and set up event listeners
    mCloud = TRTCCloud.sharedInstance(getApplicationContext());
    mCloud.setListener(this);

    Step 3. Listen for SDK events

    You can use the setListener API to set callbacks and listen for errors, warnings, traffic statistics, and network quality information as well as various audio/video events during SDK operations.

    You can make your business class inherit **TRTCCloudListener**, reload the `onError` function, and use the **setListener** API to pass in the `this` pointer to the SDK, so as to listen for callback events from the SDK in the current class.
    <dx-code-holder data-codeindex="0"></dx-code-holder>

    Step 4. Prepare the room entry parameter TRTCParams

    When calling the enterRoom API, you need to enter two key parameters: TRTCParams and TRTCAppScene.

    Parameter 1: TRTCAppScene

    This parameter is used to specify whether your application scenario is live streaming or real-time call.

    • Real-time call:
      Real-time call includes TRTCAppSceneVideoCall (video call) and TRTCAppSceneAudioCall (audio call). This mode is suitable for one-to-one audio/video calls or online meetings for up to 300 attendees.

    • Live streaming:
      Live streaming includes TRTCAppSceneLIVE (video live streaming) and TRTCAppSceneVoiceChatRoom (audio live streaming). This mode is suitable for live streaming to up to 100,000 users. However, it requires you to specify the role field in the TRTCParams parameter for users in the room: anchor or audience.

    Parameter 2: TRTCParams

    TRTCParams consists of many fields; however, you usually only need to pay attention to how to set the following fields:

    Parameter Description Remarks Data Type Sample Value
    SDKAppID Application ID You can view the SDKAppID in the TRTC console. If it doesn't exist, click Create Application to create an application. Numeric 1400000123
    userId User ID Username. It can contain only letters, digits, underscores, and hyphens. In TRTC, a user cannot use the same userId to enter the same room on two different devices at the same time. String denny or 123321
    userSig Room entry signature You can calculate userSig based on SDKAppID and userId as instructed in UserSig. String eJyrVareCeYrSy1SslI...
    roomId Room ID Room ID of the numeric type. To use a string-type room ID, use only the strRoomId field instead of the roomId field, as they cannot be used together. Number 29834
    strRoomId Room ID Room ID of the string type. Do not use strRoomId and roomId at the same time. "123" and 123 are considered different rooms by the TRTC backend. String 29834
    role Role There are two roles: anchor and audience. This field is required only when TRTCAppScene is set to the TRTCAppSceneLIVE or TRTCAppSceneVoiceChatRoom live streaming scenarios. Enumeration TRTCRoleAnchor or TRTCRoleAudience
    Note:

    • In TRTC, a user cannot use the same userId to enter the same room on two different devices at the same time; otherwise, there will be a conflict.
    • The value of appScene must be the same on each client. Inconsistent appScene may cause unexpected problems.

    Step 5. Enter the room (enterRoom)

    After preparing the two parameters TRTCAppScene and TRTCParams described in step 4, you can call the enterRoom API to enter the room.

    mCloud = TRTCCloud.sharedInstance(getApplicationContext());
    mCloud.setListener(mTRTCCloudListener);

    // Assemble TRTC room entry parameters. Replace the field values in `TRTCParams` with your own parameter values
    // Replace each field in TRTCParams with your own parameters
    TRTCCloudDef.TRTCParams param = new TRTCCloudDef.TRTCParams();
    params.sdkAppId = 1400000123; // Replace with your own SDKAppID
    params.userId = "denny"; // Replace with your own user ID
    params.roomId = 123321; // Replace with your own room number
    params.userSig = "xxx"; // Replace with your own userSig
    params.role = TRTCCloudDef.TRTCRoleAnchor;

    // If your scenario is live streaming, set the application scenario to `TRTC_APP_SCENE_LIVE`
    // If your application scenario is a group video call, use "TRTC_APP_SCENE_LIVE"
    mCloud.enterRoom(param, TRTCCloudDef.TRTC_APP_SCENE_LIVE);

    Event callback
    If room entry succeeds, the SDK will call back the onEnterRoom(result) event. Here, result is a value greater than 0, indicating the time in milliseconds taken to enter the room.
    If room entry fails, the SDK will also call back the onEnterRoom(result) event, but the value of result will be a negative number, indicating the error code for the room entry failure.

    // Listen for the `onEnterRoom` event of the SDK to get the room entry result
    // Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered
    @Override
    public void onEnterRoom(long result) {
    if (result > 0) {
    Log.d(TAG, "Enter room succeed");
    } else {
    Log.d(TAG, "Enter room failed");
    }
    }
    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