tencent cloud

文档反馈

最后更新时间:2023-12-22 15:55:27

    概述

    欢迎阅读适用于 Android 平台的 Twilio Video 到 TRTC 的迁移指南。本迁移指南将为应用程序从 Twilio 切换到 TRTC 提供无缝过渡。为了确保顺利的迁移过程,本指南在实现接入基础 RTC 视频功能的每个步骤中,都提供了 Twilio 和 TRTC 之间的 API 比较。
    如果您只是计划开发一个新应用程序,而不是进行迁移,您可以直接访问 TRTC 首页文档 您将找到开始新项目所需的所有关于 TRTC 的资料。

    创建应用

    创建 一个新的 TRTC 应用程序,然后您可以获得用于 TRTC 服务认证的以下必要信息。

    SDKAppID

    SDKAppID 是 TRTC 应用程序的唯一标识。在创建应用程序时会自动生成。

    SDKSecretKey

    SDKSecretKey 是用于生成安全签名的关键参数之一。生成的签名确保在调用 SDK 的初始化和登录 API 时访问 TRTC 服务。

    安装并设置 SDK

    环境要求

    Android Studio 3.5 or later.
    Android 4.1 (SDK API level 16) or later

    安装 SDK

    TRTC SDK 已发布到 mavenCentral 仓库,您可以按照以下步骤配置 Gradle 来下载 SDK,
    1. 将 TRTC SDK 依赖项添加到依赖项中。
    dependencies { implementation 'com.tencent.liteav:LiteAVSDK_TRTC:latest.release' }
    2. defaultConfig中,指定您的应用程序要使用的 CPU 架构。
    defaultConfig { ndk { abiFilters "armeabi-v7a", "arm64-v8a" } }
    3. 点击sync以下载 SDK 并将它们集成到您的项目中。

    项目配置

    在 AndroidManifest.xml 中配置应用程序权限。TRTC SDK 需要以下权限。
    <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera.autofocus" />

    设置混淆规则

    在 proguard-rules.pro 文件中,将与 TRTC SDK 相关的类添加到“do not obfuscate”列表中。
    -keep class com.tencent.** { *;}

    加入房间

    Twilio
    // Join room val connectOptions = ConnectOptions.Builder(accessToken) .roomName(roomName) .audioTracks(localAudioTracks) .videoTracks(localVideoTracks) .dataTracks(localDataTracks) .build() val room = Video.connect(context, connectOptions, roomListener)
    TRTC
    //Initialize TRTC // Create trtc instance(singleton) and set up event listeners val mCloud = TRTCCloud.sharedInstance(getApplicationContext()); mCloud.setListener(this); // Join room // Please replace each field in TRTCParams with your own parameters val params = TRTCParams() params.sdkAppId = 1400000123 // Please replace with your own SDKAppID params.userId = "denny" // Please replace with your own userid params.roomId = 123321 // Please replace with your own room number params.userSig = "xxx" // Please replace with your own userSig params.role = TRTCCloudDef.TRTCRoleAnchor // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE" mCloud.enterRoom(params, TRTCCloudDef.TRTC_APP_SCENE_LIVE) // 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"); } }

    发布本地视频/音频

    Twilio
    // Start video val cameraEnumerator = Camera2Enumerator(context) cameraEnumerator.deviceNames.firstOrNull { cameraEnumerator.isFrontFacing(it) }?.let { val cameraCapturer = Camera2Capturer(context, it) val localVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer) cameraCapturer.startCapture(width, height, framerate) } // Stop video cameraCapturer.stopCapture()
    TRTC
    // Set the preview mode of the local video image: Enable horizontal mirroring and set the fill mode for the video image TRTCCloudDef.TRTCRenderParams param = new TRTCCloudDef.TRTCRenderParams(); param.fillMode = TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL; param.mirrorType = TRTCCloudDef.TRTC_VIDEO_MIRROR_TYPE_AUTO; mCloud.setLocalRenderParams(param); // Enable local camera preview (`localCameraVideo` is the control used to render the local video image) TXCloudVideoView cameraVideo = findViewById(R.id.txcvv_main_local); mCloud.startLocalPreview(true, cameraVideo); // Use `TXDeviceManager` to enable autofocus and turn on the flash TXDeviceManager manager = mCloud.getDeviceManager(); if (manager.isAutoFocusEnabled()) { manager.enableCameraAutoFocus(true); } manager.enableCameraTorch(true); // Enable mic and set `quality` to `SPEECH` for the voice mode mCloud.startLocalAudio(TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH );

    订阅远程视频/音频

    Twilio
    private lateinit var remoteVideoView: VideoView private fun setRoomListener() { remoteVideoView = findViewById(R.id.remote_video_view) val roomListener = object : Room.Listener { override fun onConnected(room: Room) { room.remoteParticipants.forEach { remoteParticipant -> addRemoteParticipant(remoteParticipant) } } override fun onParticipantConnected(room: Room, remoteParticipant: RemoteParticipant) { addRemoteParticipant(remoteParticipant) } } } private fun addRemoteParticipant(remoteParticipant: RemoteParticipant) { remoteParticipant.setListener(object : RemoteParticipant.Listener { override fun onVideoTrackSubscribed( remoteParticipant: RemoteParticipant, remoteVideoTrackPublication: RemoteVideoTrackPublication, remoteVideoTrack: RemoteVideoTrack ) { remoteVideoTrack.addSink(remoteVideoView) } override fun onVideoTrackUnsubscribed( remoteParticipant: RemoteParticipant, remoteVideoTrackPublication: RemoteVideoTrackPublication, remoteVideoTrack: RemoteVideoTrack ) { remoteVideoTrack.removeSink(remoteVideoView) } }) }
    TRTC
    // Play back the camera (primary stream) image of `denny` mCloud.startRemoteView("denny", TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG, cameraView);

    离开房间

    Twilio
    room.disconnect()
    TRTC
    // Exit the current room mCloud.exitRoom(); // Listen for the `onExitRoom` callback to get the reason for room exit @Override public void onExitRoom(int reason) { if (reason == 0) { Log.d(TAG, "Exit current room by calling the 'exitRoom' api of sdk ..."); } else if (reason == 1) { Log.d(TAG, "Kicked out of the current room by server through the restful api..."); } else if (reason == 2) { Log.d(TAG, "Current room is dissolved by server through the restful api..."); } }

    总结

    通过与 Twilio Video API 的接入对照,本迁移指南概述了如何使用 Tencent RTC (TRTC) 为 Android 平台构建基本的视频 RTC 体验。本指南中每个步骤的 API 级别“映射”将帮助开发者快速、直接地从 Twilio Video 切换到 TRTC。
    TRTC 有更多与音视频的功能及服务,可以帮助开发者实现经济高效、低延迟和高质量的互动音视频服务。有关 TRTC 功能和实现规范的详细信息,请参阅Tencent RTC 官方网站。如果您需要开发者支持或关于 TRTC 集成的任何进一步帮助,请随时 联系我们,或者您可以加入我们的 DiscordTelegram。我们将确保顺利集成并解答您可能遇到的任何问题。
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持