概述
欢迎阅读适用于 iOS 平台的 Twilio Video 到 TRTC 的迁移指南。本迁移指南将为应用程序从 Twilio 切换到 TRTC 提供无缝过渡。为了确保顺利的迁移过程,本指南在实现接入基础 RTC 视频功能的每个步骤中,都提供了 Twilio 和 TRTC 之间的 API 比较。
如果您只是计划开发一个新应用程序,而不是进行迁移,您可以直接访问 TRTC 首页 和 文档 您将找到开始新项目所需的所有关于 TRTC 的资料。 创建应用
创建一个新的 TRTC 应用程序,然后您可以获得用于 TRTC 服务认证的以下必要信息。 SDKAppID
SDKAppID 是 TRTC 应用程序的唯一标识。在创建应用程序时会自动生成。
SDKSecretKey
SDKSecretKey 是用于生成安全签名的关键参数之一。生成的签名确保在调用 SDK 的初始化和登录 API 时访问 TRTC 服务。
安装并设置 SDK
环境要求
Xcode 9.0 或更高版本
配备 iOS 9.0 或更高版本的 iPhone 或 iPad
项目的有效开发者签名
安装 SDK
您可以选择使用 CocoaPods 或手动下载并将 SDK 导入到您的项目中。如果使用 CocoaPods,请确保已安装 CocoaPods,并通过以下命令创建 Podfile:
sudo gem install cocoapods
pod init
编辑创建的 Podfile,并根据您的项目需求填写一个 TRTC 版本:
pod 'TXLiteAVSDK_TRTC', 'version'
然后通过以下命令更新本地仓库并安装 SDK:
项目配置
要使用 SDK 的音频/视频功能,您需要为应用程序授予麦克风和相机权限。将两个项目(隐私 - 麦克风使用说明和隐私 - 相机使用说明)添加到您的 Xcode 应用程序的 Info.plist 中。
导入 SDK
完成上述步骤后,您可以根据需要通过以下方式将 TRTC SDK 提供的 API 导入到您的项目中。
1. 使用 Objective-C 或 Swift API
// Import the SDK module
@import TXLiteAVSDK_TRTC;
// Import the header file
#import "TXLiteAVSDK_TRTC/TRTCCloud.h"
2. 使用 C++ API
#include "TXLiteAVSDK_TRTC/cpp_interface/ITRTCCloud.h"
进入房间
@IBAction func createARoom(sender: AnyObject) {
let connectOptions = ConnectOptions(token: accessToken) { (builder) in
builder.roomName = "my-room"
}
room = TwilioVideoSDK.connect(options: connectOptions, delegate: self)
}
// MARK: RoomDelegate
func roomDidConnect(room: Room) {
print("Did connect to Room")
if let localParticipant = room.localParticipant {
print("Local identity \\(localParticipant.identity)")
// Set the delegate of the local particiant to receive callbacks
localParticipant.delegate = self
}
}
// Create trtc instance(singleton) and set up event listeners
self.trtcCloud = [TRTCCloud sharedInstance];
self.trtcCloud.delegate = self;
// Package the room entry parameter
// Please replace each field in TRTCParams with your own parameters
TRTCParams *params = [[TRTCParams alloc] init];
params.sdkAppId = 1400000123; // Please replace with your own SDKAppID
params.roomId = 123321; // Please replace with your own room number
params.userId = @"denny"; // Please replace with your own userid
params.userSig = @"xxx"; // Please replace with your own userSig
params.role = TRTCRoleAnchor;
// If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
[self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
// Listen to the onEnterRoom event of the SDK and get notification whether the room is successfully entered
- (void)onEnterRoom:(NSInteger)result {
if (result > 0) {
[self toastTip:@"Enter room succeed!"];
} else {
[self toastTip:@"Enter room failed!"];
}
}
发布本地视频/音频
T// start camera
var cameraSource = CameraSource(delegate: self),
let captureDevice = CameraSource.captureDevice(position: .front)
cameraSource.startCapture(device: captureDevice, completion: nil)
// Use CameraSource to produce video from the device's front camera.
if let camera = TVICameraCapturer(source: .frontCamera),
let videoTrack = TVILocalVideoTrack(capturer: camera) {
// TVIVideoView is a TVIVideoRenderer and can be added to any TVIVideoTrack.
let renderer = TVIVideoView(frame: view.bounds)
// Add renderer to the video track
videoTrack.addRenderer(renderer)
self.localVideoTrack = videoTrack
self.camera = camera
self.view.addSubview(renderer)
}
self.trtcCloud = [TRTCCloud sharedInstance];
// Set the preview mode of the local video image: Enable horizontal mirroring and set the fill mode for the video image
TRTCRenderParams *param = [[TRTCRenderParams alloc] init];
param.fillMode = TRTCVideoFillMode_Fill;
param.mirrorType = TRTCVideoMirrorTypeAuto;
[self.trtcCloud setLocalRenderParams:param];
// Enable local camera preview(`localCameraVideoView` is used to render the local video image)
[self.trtcCloud startLocalPreview:YES view:localCameraVideoView];
// Enable mic and set `quality` to `SPEECH` for the voice mode
[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
订阅远程视频/音频
// MARK: RemoteParticipantDelegate
/*
* In the Participant Delegate, we can respond when the Participant adds a Video
* Track by rendering it on screen.
*/
func didSubscribeToVideoTrack(videoTrack: RemoteVideoTrack,
publication: RemoteVideoTrackPublication,
participant: RemoteParticipant) {
if let remoteView = VideoView.init(frame: self.view.bounds,
delegate:self) {
videoTrack.addRenderer(remoteView)
self.view.addSubview(remoteView)
self.remoteView = remoteView
}
}
// MARK: VideoViewDelegate
// Lastly, we can subscribe to important events on the VideoView
func videoViewDimensionsDidChange(view: VideoView, dimensions: CMVideoDimensions) {
self.view.setNeedsLayout()
}
self.trtcCloud = [TRTCCloud sharedInstance];
// Play back the camera (primary stream) image of `denny`
[self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
离开房间
//TodisconnectfromaRoom,wecall:
room?.disconnect()
//ThisresultsinacallbacktoRoomDelegate#roomDidDisconnect(room:Room,error:Error?)
//MARK:RoomDelegate
funcroomDidDisconnect(room:Room,error:Error?){
print("Disconnectedfromroom\\(room.name)")
}
self.trtcCloud = [TRTCCloud sharedInstance];
// Exit the current room
[self.trtcCloud exitRoom];
// Listen the `onExitRoom` callback to get notification
- (void)onExitRoom:(NSInteger)reason {
if (reason == 0) {
NSLog(@"Exit current room by calling the 'exitRoom' api of sdk ...");
} else if (reason == 1) {
NSLog(@"Kicked out of the current room by server through the restful api...");
} else if (reason == 2) {
NSLog(@"Current room is dissolved by server through the restful api...");
}
}
总结
通过与 Twilio Video API 的接入对照,本迁移指南概述了如何使用 Tencent RTC (TRTC) 为 iOS 平台构建基本的视频 RTC 体验。本指南中每个步骤的 API 级别“映射”将帮助开发者快速、直接地从 Twilio Video 切换到 TRTC。
TRTC 有更多与音视频的功能及服务,可以帮助开发者实现经济高效、低延迟和高质量的互动音视频服务。有关 TRTC 功能和实现规范的详细信息,请参阅Tencent RTC 官方网站。如果您需要开发者支持或关于 TRTC 集成的任何进一步帮助,请随时 联系我们,或者您可以加入我们的 Discord 和 Telegram。我们将确保顺利集成并解答您可能遇到的任何问题。
本页内容是否解决了您的问题?