Note:
The TUIVoiceRoom component has been discontinued. If your business scenario requires integrating voice chat functionality, you can use the new version of the voice chat room (including UI) called TUILiveKit. 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
:
Add dependencies on Source
to the build.gradle
file in app
:
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"
}
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
TRTCVoiceRoom mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(this);
mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
);
mTRTCVoiceRoom.login(SDKAppID, userId, userSig, new TRTCVoiceRoomCallback.ActionCallback() {
@Override
public void onCallback(int code, String msg) {
if (code == 0) {
}
}
});
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, SDKAppID
information is as shown in the figure below:
SDKSecretkey: The TRTC application key. Each secret key corresponds to a SDKAppID
. The SecretKey information is shown in the above figure.
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
int roomId = 12345;
final TRTCVoiceRoomDef.RoomParam roomParam = new TRTCVoiceRoomDef.RoomParam();
roomParam.roomName = "Room name";
roomParam.needRequest = false;
roomParam.seatCount = 7;
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) {
}
}
});
mTRTCVoiceRoom.enterRoom(roomId, new TRTCVoiceRoomCallback.ActionCallback() {
@Override
public void onCallback(int code, String msg) {
if (code == 0) {
}
}
});
int seatIndex = 2;
mTRTCVoiceRoom.enterSeat(seatIndex, new TRTCVoiceRoomCallback.ActionCallback() {
@Override
public void onCallback(int code, String msg) {
if (code == 0) {
}
}
});
@Override
public void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {
}
int seatIndex = 2;
String userId = "123";
mTRTCVoiceRoom.pickSeat(1, userId, new TRTCVoiceRoomCallback.ActionCallback() {
@Override
public void onCallback(int code, String msg) {
if (code == 0) {
}
}
});
@Override
public void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {
}
String seatIndex = "1";
String userId = "123";
String inviteId = mTRTCVoiceRoom.sendInvitation("takeSeat", userId, seatIndex, null);
@Override
public void onInviteeAccepted(String id, String invitee) {
if(id.equals(inviteId)) {
mTRTCVoiceRoom.enterSeat(index, null);
}
}
@Override
public void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {
if (cmd.equals("takeSeat")) {
mTRTCVoiceRoom.acceptInvitation(id, null);
}
}
String seatIndex = "1";
String userId = "123";
String inviteId = mTRTCVoiceRoom.sendInvitation("pickSeat", userId, seatIndex, null);
@Override
public void onInviteeAccepted(String id, String invitee) {
if(id.equals(inviteId)) {
mTRTCVoiceRoom.pickSeat(index, null);
}
}
@Override
public void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {
if (cmd.equals("pickSeat")) {
mTRTCVoiceRoom.acceptInvitation(id, null);
}
}
mTRTCVoiceRoom.sendRoomTextMsg("Hello World!", null);
mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
@Override
public void onRecvRoomTextMsg(String message, TRTCVoiceRoomDef.UserInfo userInfo) {
Log.d(TAG, "Received a message from" + userInfo.userName + ": " + message);
}
});
mTRTCVoiceRoom.sendRoomCustomMsg("CMD_DANMU", "Hello world", null);
mTRTCVoiceRoom.sendRoomCustomMsg("CMD_LIKE", "", null);
mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {
@Override
public void onRecvRoomCustomMsg(String cmd, String message, TRTCVoiceRoomDef.UserInfo userInfo) {
if ("CMD_DANMU".equals(cmd)) {
Log.d(TAG, "Received an on-screen comment from" + userInfo.userName + ": " + message);
} else if ("CMD_LIKE".equals(cmd)) {
Log.d(TAG, userInfo.userName + "liked you.");
}
}
});
Suggestions and Feedback
If you have any questions or encounter any issues related to Tencent RTC, we encourage you to post your questions on our Stackoverflow community forum or Telegram group. Our team and other community members are on standby to help you.
Was this page helpful?