This tutorial mainly introduces how to implement a basic audio and video call.
Prerequisites
Xcode 9.0 or later
iPhone or iPad with iOS 9.0 or later
A valid developer signature for your project
Step 1. Import TRTC SDK
1. Run the following command in the terminal window to install CocoaPods. If you have installed CocoaPods, skip this step.
sudo gem install cocoapods
2. After going to the TRTCDemo root directory, enter the following command to create the Podfile file for your project.
3. Edit the Podfile file as follows and change the App to the name of your own project.
platform :ios, '8.0'
target 'App' do
pod 'TXLiteAVSDK_TRTC', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_TRTC.podspec'
end
4. Enter the following command to update the local library file and install the SDK.
Note:
After the pod command is executed, a project file with the .xcworkspace suffix integrated with the SDK is generated. Double-click the .xcworkspace file to open it.
Step 2. Configure project
1. After opening the .xcworkspace file, add TXLiteAVSDK_TRTC.xcframework to the Frameworks, Libraries, and Embedded Content section in General tab.
2. Search for User Script Sandboxing in Build Settings tab and set its value to No.
3. Add Privacy-Microphone Usage Description and Privacy-Microphone Usage Description to Info.plist tab, and fill in the target prompt words used by the Microphone/Camera to obtain the permissions to use the microphone and camera.
4. Add Background Modes to the Signing & Capabilities tab and check Audio, AirPlay and Picture in Picture.
Step 3. Import Module
Add a module reference to the SDK in the AppDelegate.h file:
@import TXLiteAVSDK_TRTC;
Step 4. Create TRTC instance
1. Add the following properties to the AppDelegate.h file and declare the toastTip:
method.
#import <UIKit/UIKit.h>
@import TXLiteAVSDK_TRTC;
@interface AppDelegate : UIResponder <UIApplicationDelegate, TRTCCloudDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) TRTCCloud *trtcCloud;
@property (nonatomic, strong) UIView *localCameraVideoView;
- (void)toastTip:(NSString *)tip;
@end
2. Implement the toastTip:
method in the AppDelegate.m file.
- (void)toastTip:(NSString *)tip {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tip: " message:tip preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Confirm: " style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
3. Call the interface to create a TRTC instance in the didFinishLaunchingWithOptions()
, and set up the event callbacks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_trtcCloud = [TRTCCloud sharedInstance];
_trtcCloud.delegate = self;
return YES;
}
- (void)onError:(TXLiteAVError)errCode
errMsg:(nullable NSString *)errMsg
extInfo:(nullable NSDictionary *)extInfo{
if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {
NSString *errorInfo = @"Current application is not authorized to use the camera:";
errorInfo = [errorInfo stringByAppendingString : errMsg];
[self toastTip:errorInfo];
}
}
Step 5. Enter the room
1. Click Create Application
in the Tencent RTC console to get the SDKAppID under Application Overview. 2. Select SDKAppID down in the UserSig Tools, enter your UserID, and click Generate
to get your own UserSig. 3. After setting the TRTCParams for room entry, call the enterRoom
to enter the room.
As an Anchor:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAnchor;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (void)onEnterRoom:(NSInteger)result {
if (result > 0) {
[self toastTip:@"Enter room succeed!"];
} else {
[self toastTip:@"Enter room failed!"];
}
}
As an audience:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAudience;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (void)onEnterRoom:(NSInteger)result {
if (result > 0) {
[self toastTip:@"Enter room succeed!"];
} else {
[self toastTip:@"Enter room failed!"];
}
}
Note:
If you enter the room as an audience, sdkAppId and roomId need to be the same as on the anchor side, while userId and userSig need to be replaced with your own values.
Step 6. Turn on Camera
1. Initialize localCameraVideoView in didFinishLaunchingWithOptions()
method, and call the setLocalRenderParams
to set the local preview render parameters.
self.localCameraVideoView = [[UIView alloc] initWithFrame:self.window.bounds];
self.localCameraVideoView.backgroundColor = [UIColor blackColor];
[self.window addSubview:self.localCameraVideoView];
TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
[self.trtcCloud setLocalRenderParams:trtcRenderParams];
[self.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];
2. Call the TXDeviceManager
to perform operations such as Switching between front and rear cameras, Setting Focus Mode, and Enabling.
TXDeviceManager *manager = [self.trtcCloud getDeviceManager];
if ([manager isAutoFocusEnabled]) {
[manager enableCameraAutoFocus:YES];
}
Note:
The front camera is turned on by default. If you need to use the rear camera, call manager.switchCamera(false)
to turn on the rear camera.
3. Add the localCameraVideoView property to ViewController.h file.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UIView *localCameraVideoView;
@end
4. Initialize the localCameraVideoView in ViewController.m file.
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.localCameraVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
self.localCameraVideoView.backgroundColor = [UIColor blackColor];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];
[self.view addSubview:self.localCameraVideoView];
}
@end
Step 7. Turn on microphone
Call startLocalAudio
to enable microphone capture. This interface requires you to determine the capture mode by the quality
parameter. It is recommended to select one of the following modes that is suitable for your project according to your needs.
[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
[self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
Step 8. Play/stop video streaming
After you enter denny's room as an audience by following steps 1-4 to create a new project, you can play a video of the remote user by calling the startRemoteView
.
[self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
Then, you can call the stopRemoteView
to stop the videos of a remote user. Alternatively, you can also stop the videos of all remote users via the stopAllRemoteView
.
[self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
[self.trtcCloud stopAllRemoteView];
Step 9. Play/stop the audio stream
Mute the voice of remote user denny by calling the muteRemoteAudio("denny", true)
.
[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
You can also unmute him later by calling the muteRemoteAudio("denny", false)
.
[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
Step 10. Exit the room
Call the exitRoom
to exit the current room, the SDK will notify you after the check-out through the onExitRoom(int reason)
callback event.
[self.trtcCloud exitRoom];
- (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...");
}
}
FAQs
Contact us
If you have any suggestions or feedback, please contact info_rtc@tencent.com
.
Was this page helpful?