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: |
TUIVoiceRoom
componentAndroid/Source
directory to your project, and complete the following import operations:setting.gradle
:include ':Source'
Source
to the build.gradle
file in app
:api project(':Source')
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"}
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" />
proguard-rules.pro
file, add the SDK classes to the "do not obfuscate" list.-keep class com.tencent.** { *;}
// 1. InitializeTRTCVoiceRoom mTRTCVoiceRoom = TRTCVoiceRoom.sharedInstance(this);mTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {);// 2. Log inmTRTCVoiceRoom.login(SDKAppID, userId, userSig, new TRTCVoiceRoomCallback.ActionCallback() {@Overridepublic void onCallback(int code, String msg) {if (code == 0) {// Logged in}}});
SDKAppID
.
SDKAppID
. You can view your application’s secret key on the Application Management page of the TRTC console.SDKAppID
, userId
, and Secretkey
. You can click here to directly generate a debugging userSig
online. For more information, see UserSig.// 1. The room owner calls an API to create a roomint roomId = 12345; // Room IDfinal TRTCVoiceRoomDef.RoomParam roomParam = new TRTCVoiceRoomDef.RoomParam();roomParam.roomName = "Room name";roomParam.needRequest = false; // Whether the room owner’s permission is required for listeners to speakroomParam.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() {@Overridepublic void onCallback(int code, String msg) {if (code == 0) {// Room created successfully}}});
// 1. A listener calls an API to enter the roommTRTCVoiceRoom.enterRoom(roomId, new TRTCVoiceRoomCallback.ActionCallback() {@Overridepublic void onCallback(int code, String msg) {if (code == 0) {// Entered room successfully}}});
// 1. A listener calls an API to mic onint seatIndex = 2; // Seat indexmTRTCVoiceRoom.enterSeat(seatIndex, new TRTCVoiceRoomCallback.ActionCallback() {@Overridepublic void onCallback(int code, String msg) {if (code == 0) {// Operation succeeded}}});// 2. The `onSeatListChange` callback is received, and the seat list is refreshed@Overridepublic void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {}
// 1. The room owner makes a listener a speakerint seatIndex = 2; // Seat indexString userId = "123"; // The ID of the user who will become a speakermTRTCVoiceRoom.pickSeat(1, userId, new TRTCVoiceRoomCallback.ActionCallback() {@Overridepublic void onCallback(int code, String msg) {if (code == 0) {// Operation succeeded}}});// 2. The `onSeatListChange` callback is received, and the seat list is refreshed@Overridepublic void onSeatListChange(final List<TRTCVoiceRoomDef.SeatInfo> seatInfoList) {}
// Listener// 1. A listener calls an API to request to speakString seatIndex = "1"; // Seat indexString userId = "123"; // User IDString inviteId = mTRTCVoiceRoom.sendInvitation("takeSeat", userId, seatIndex, null);// 2. Place the user in the seat after the invitation is accepted@Overridepublic void onInviteeAccepted(String id, String invitee) {if(id.equals(inviteId)) {mTRTCVoiceRoom.enterSeat(index, null);}}// Room owner// 1. The room owner receives the request@Overridepublic void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {if (cmd.equals("takeSeat")) {// 2. The room owner accepts the requestmTRTCVoiceRoom.acceptInvitation(id, null);}}
// Room owner// 1. The room owner calls an API to invite a listener to speakString seatIndex = "1"; // Seat indexString userId = "123"; // User IDString inviteId = mTRTCVoiceRoom.sendInvitation("pickSeat", userId, seatIndex, null);// 2. Place the user in the seat after the invitation is accepted@Overridepublic void onInviteeAccepted(String id, String invitee) {if(id.equals(inviteId)) {mTRTCVoiceRoom.pickSeat(index, null);}}// Listener// 1. The listener receives the invitation@Overridepublic void onReceiveNewInvitation(final String id, String inviter, String cmd, final String content) {if (cmd.equals("pickSeat")) {// 2. The listener accepts the invitationmTRTCVoiceRoom.acceptInvitation(id, null);}}
// Sender: Sends a text chat messagemTRTCVoiceRoom.sendRoomTextMsg("Hello World!", null);// Receiver: Listens for text chat messagesmTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {@Overridepublic void onRecvRoomTextMsg(String message, TRTCVoiceRoomDef.UserInfo userInfo) {Log.d(TAG, "Received a message from" + userInfo.userName + ": " + message);}});
// 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 messagesmTRTCVoiceRoom.setDelegate(new TRTCVoiceRoomDelegate() {@Overridepublic void onRecvRoomCustomMsg(String cmd, String message, TRTCVoiceRoomDef.UserInfo userInfo) {if ("CMD_DANMU".equals(cmd)) {// An on-screen comment is receivedLog.d(TAG, "Received an on-screen comment from" + userInfo.userName + ": " + message);} else if ("CMD_LIKE".equals(cmd)) {// A like is receivedLog.d(TAG, userInfo.userName + "liked you.");}}});
Was this page helpful?