Android

最近更新时间:2024-06-24 10:43:31

我的收藏
本文将指导您如何在较短时间内接入TUIRoomKit组件。遵循本指南,您将在10分钟内完成以下关键步骤,并最终实现具备完整 UI 界面的音视频会议功能。
会议界面及部分功能效果展示




环境准备

最低兼容 Android 4.4(SDK API Level 19),建议使用 Android 5.0 (SDK API Level 21)及以上版本。
Android Studio 3.5 及以上的版本(Gradle 3.5.4 及以上的版本)。
Android 4.4 及以上的手机设备。

步骤一:开通服务

在使用 TUIRoomKit 发起会议前,您需要前往控制台开通 TUIRoomKit 专属的多人音视频互动服务,具体步骤请参见 开通服务

步骤二:下载 TUIRoomKit 组件

1. Github 中克隆/下载代码,然后拷贝Android目录下的timcommontuiroomkit子目录到您当前工程中的 app 同级目录中,如下图:




步骤三:工程配置

1. 工程根目录下找到setting.gradle(或settings.gradle.kts)文件,并在其中增加如下代码,它的作用是将tuiroomkit组件导入到您当前的项目中。
settings.gradle
setting.gradle.kts
include ':timcommon'
include ':tuiroomkit'
include (":timcommon") include (":tuiroomkit")
2. 在 app 目录下找到build.gradle(或build.gradle.kts)文件,并在其中增加如下代码,它的作用是声明当前app对新加入的tuiroomkit组件的依赖。
build.gradle
build.gradle.kts
api project(':tuiroomkit')
api(project(":tuiroomkit"))
3. 由于我们在 SDK 内部使用了Java 的反射特性,需要将 SDK 中的部分类加入不混淆名单,因此需要您在 proguard-rules.pro 文件中添加如下代码:
-keep class com.tencent.** { *; }
4. 在 app目录下找到 AndroidManifest.xml 文件,在 application 节点中添加 tools:replace="android:allowBackup" ,覆盖组件内的设置,使用自己的设置。
// app/src/main/AndroidManifest.xml
<application android:name=".DemoApplication" android:allowBackup="false" android:icon="@drawable/app_ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:theme="@style/AppTheme" tools:replace="android:allowBackup">

步骤四:登录

在您的项目中添加如下代码,它的作用是通过调用TUILogin中的相关接口完成组件的登录。这个步骤异常关键,因为只有在登录后才能正常使用 TUIRoomKit的各项功能,故请您耐心检查相关参数是否配置正确。
Java
Kotlin
import com.tencent.qcloud.tuicore.TUILogin; import com.tencent.qcloud.tuicore.interfaces.TUICallback;
import com.tencent.cloud.tuikit.roomkit.debug.GenerateTestUserSig;

String userId = "denny"; // 请替换为您的 UserID
int sdkAppId = 1400000001; // 请替换为步骤一取到的 sdkAppId
String sdkSecretKey = "xxxx"; // 请替换为您的 sdkSecretKey
String userSig = GenerateTestUserSig.genTestUserSig(sdkAppId, userId, sdkSecretKey);

TUILogin.login(context,
sdkAppId,
userId,
userSig,
new TUICallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(int errorCode, String errorMessage) {
}
});
import com.tencent.qcloud.tuicore.TUILogin import com.tencent.qcloud.tuicore.interfaces.TUICallback
import com.tencent.cloud.tuikit.roomkit.debug.GenerateTestUserSig

val userId = "denny" // 请替换为您的 UserID
val sdkAppId = 1400000001 // 请替换为步骤一取到的 sdkAppId
val sdkSecretKey = "xxxx" // 请替换为您的 sdkSecretKey
val userSig = GenerateTestUserSig.genTestUserSig(sdkAppId, userId, sdkSecretKey)

TUILogin.login(this,
sdkAppId,
userId,
userSig,
object : TUICallback() {
override fun onSuccess() {
}

override fun onError(errorCode: Int, errorMessage: String) {
}
})
TUILogin.login 函数参数说明:
SDKAppID:在 开通服务 中的最后一步中获取。
UserID:当前用户的 ID,字符串类型,只允许包含英文字母(a-z 和 A-Z)、数字(0-9)、连词符(-)和下划线(_)。
UserSig:使用 开通服务 的第3步中获取的SDKSecretKeySDKAppIDUserID等信息进行加密,就可以得到UserSig,它是一个鉴权用的票据,用于腾讯云识别当前用户是否能够使用TRTC的服务。您可以通过控制台中的 辅助工具 生成一个临时可用的UserSig
更多信息请参见 如何计算及使用 UserSig
说明:
开发环境:如果您正在本地开发调试阶段,可以采用本地 GenerateTestUserSig.genTestUserSig() 函数生成 userSig。该方法中 SDKSecretKey 很容易被反编译逆向破解,一旦您的密钥泄露,攻击者就可以盗用您的腾讯云流量。
生产环境:如果您的项目要发布上线,请采用 服务端生成 UserSig 的方式。

步骤五:您发起一次会议

1. 在您项目目录 app/src/main/res/layout (若没有layout目录,可以新建一个) 新建 activity_conference.xml 文件,并添加界面布局:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/conference_container" android:layout_width="match_parent" android:layout_height="match_parent" />
2. ConferenceMainFragment 是会议的主界面,只需调用 quickStartConference 方法(在该方法中传入自定义的房间号)发起快速会议,并在快速会议的回调 onConferenceStarted 中将 ConferenceMainFragment 添加到当前 Activity 中,即可发起快速会议。
在项目中新建 ConferenceOwnerActivity.java 文件。
Java
Kotlin
import com.tencent.cloud.tuikit.roomkit.ConferenceError; import com.tencent.cloud.tuikit.roomkit.ConferenceMainFragment; import com.tencent.cloud.tuikit.roomkit.ConferenceObserver;

public class ConferenceOwnerActivity extends AppCompatActivity {
private ConferenceObserver mConferenceObserver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conference);

ConferenceMainFragment fragment = new ConferenceMainFragment();
mConferenceObserver = new ConferenceObserver() {
@Override
public void onConferenceStarted(String conferenceId, ConferenceError error) {
super.onConferenceStarted(conferenceId, error);
if (error != ConferenceError.SUCCESS) {
return;
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.conference_container, fragment);
transaction.commitAllowingStateLoss();
}
};
fragment.setConferenceObserver(mConferenceObserver);
// 请替换 "123456" 为你自定义的房间号
fragment.quickStartConference("123456");
}
}
import com.tencent.cloud.tuikit.roomkit.ConferenceError import com.tencent.cloud.tuikit.roomkit.ConferenceMainFragment import com.tencent.cloud.tuikit.roomkit.ConferenceObserver

class ConferenceOwnerActivity : AppCompatActivity() { private var mConferenceObserver : ConferenceObserver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_conference) var fragment = ConferenceMainFragment() mConferenceObserver = object: ConferenceObserver() { override fun onConferenceStarted(conferenceId: String?, error: ConferenceError?) { super.onConferenceStarted(conferenceId, error) if (error != ConferenceError.SUCCESS) { return } val fragmentManager = supportFragmentManager val transaction = fragmentManager.beginTransaction() transaction.add(R.id.conference_container, fragment) transaction.commitAllowingStateLoss() } } fragment.setConferenceObserver(mConferenceObserver) // 请替换 "123456" 为你自定义的房间号 fragment.quickStartConference("123456") } }
在 app/src/main/AndroidManifest.xml 文件中注册 ConferenceOwnerActivity。
<!--app/src/main/AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application ... <activity ... </activity>
<!-- 添加如下代码注册 ConferenceOwnerActivity --> <activity android:name=".ConferenceOwnerActivity" /> </application> </manifest>
注意:
ConferenceOwnerActivity 必须继承 AppCompatActivity,否则界面将无法显示。
ConferenceOwnerActivity 必须在 AndroidManifest.xml 文件中注册。
3. 在您调用过 TUILogin.login 函数 后,可根据您的业务,在需要发起会议的地方拉起 ConferenceOwnerActivity 会议界面。例如,在一个空项目的 MainActivity.java 中拉起会议界面的示例代码如下:
Java
Kotlin
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
// 拉起会议界面 Intent intent = new Intent(MainActivity.this, ConferenceOwnerActivity.class); startActivity(intent); } }
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 拉起会议界面
val intent = Intent(this, ConferenceOwnerActivity::class.java)
startActivity(intent)
}
}

步骤六:会议成员加入会议

1. ConferenceMainFragment 是会议的主界面,只需调用 joinConference 方法(在该方法中传入您要加入的房间号)加入会议,并在加入会议的回调 onConferenceJoined 中将 ConferenceMainFragment 添加到当前 Activity 中,即可参与当前会议。
在项目中新建 ConferenceGeneralActivity.java 文件。
Java
Kotlin
import android.util.Log;
import com.tencent.cloud.tuikit.roomkit.ConferenceError; import com.tencent.cloud.tuikit.roomkit.ConferenceMainFragment; import com.tencent.cloud.tuikit.roomkit.ConferenceObserver;

public class ConferenceGeneralActivity extends AppCompatActivity {
private ConferenceObserver mConferenceObserver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conference);

ConferenceMainFragment fragment = new ConferenceMainFragment();
mConferenceObserver = new ConferenceObserver() {
@Override
public void onConferenceJoined(String conferenceId, ConferenceError error) {
super.onConferenceJoined(conferenceId, error);
if (error != ConferenceError.SUCCESS) {
return;
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.conference_container, fragment);
transaction.commitAllowingStateLoss();
}
};
fragment.setConferenceObserver(mConferenceObserver);
// 请替换 "123456" 为您要加入的房间号
fragment.joinConference("123456");
}
}
import android.util.Log
import com.tencent.cloud.tuikit.roomkit.ConferenceError import com.tencent.cloud.tuikit.roomkit.ConferenceMainFragment import com.tencent.cloud.tuikit.roomkit.ConferenceObserver

class ConferenceGeneralActivity : AppCompatActivity() { private val tag: String = "ConferenceGeneralActivity" private var mConferenceObserver : ConferenceObserver? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_conference) var fragment = ConferenceMainFragment() mConferenceObserver = object: ConferenceObserver() { override fun onConferenceJoined(conferenceId: String?, error: ConferenceError?) { super.onConferenceJoined(conferenceId, error) if (error != ConferenceError.SUCCESS) { Log.e(tag, "Error : $error") return } val fragmentManager = supportFragmentManager val transaction = fragmentManager.beginTransaction() transaction.add(R.id.conference_container, fragment) transaction.commitAllowingStateLoss() } } fragment.setConferenceObserver(mConferenceObserver) // Replace "123456" with the corresponding conference number fragment.joinConference("123456") } }
在 app/src/main/AndroidManifest.xml 文件中注册 ConferenceGeneralActivity。
<!--app/src/main/AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application ... <activity ... </activity>
<!-- 添加如下代码注册 ConferenceOwnerActivity --> <activity android:name=".ConferenceOwnerActivity" />
<!-- 添加如下代码注册 ConferenceGeneralActivity -->
<activity android:name=".ConferenceGeneralActivity" /> </application> </manifest>
注意:
ConferenceGeneralActivity 必须继承 AppCompatActivity,否则界面将无法显示。
ConferenceGeneralActivity 必须在 AndroidManifest.xml 文件中注册。
2. 在您调用过 TUILogin.login 函数后,可根据您的业务,在需要加入会议的地方拉起 ConferenceGeneralActivity 会议界面。例如,在一个空项目的 MainActivity.java 中拉起加入会议界面的示例代码如下:
Java
Kotlin
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
// 拉起会议界面 Intent intent = new Intent(MainActivity.this, ConferenceGeneralActivity.class); startActivity(intent); } }
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 拉起会议界面
val intent = Intent(this, ConferenceGeneralActivity::class.java)
startActivity(intent)
}
}

界面展示

当您成功完成步骤1 - 步骤6,UI 界面效果如下:
会议主界面
用户列表







常见问题

如果您的接入和使用中遇到问题,请参见 常见问题

交流与反馈

如果您在接入和使用过程有任何需要或者反馈,欢迎加入我们的 TUIRoomKit 技术交流平台 zhiliao,进行技术交流和问题反馈。