tencent cloud

Feedback

Last updated: 2024-07-18 15:20:21
    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.
    pod init
    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.
    pod install
    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; // Add the window property
    @property (nonatomic, strong) TRTCCloud *trtcCloud; // Add the trtcCloud property
    @property (nonatomic, strong) UIView *localCameraVideoView; // Add the localCameraVideoView property
    
    - (void)toastTip:(NSString *)tip; // Declare the toastTip: method
    @end
    
    2. Implement the toastTip: method in the AppDelegate.m file.
    // Implement the toastTip: method
    - (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 {
    // Override point for customization after application launch.
    // Create trtc instance(singleton) and set up event listeners
    _trtcCloud = [TRTCCloud sharedInstance];
    _trtcCloud.delegate = self;
    return YES;
    }
    
    // Listen to the "onError" event, and print logs for errors such as "Camera is not authorized"
    - (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 {
    // Override point for customization after application launch.
    // ...Other codes
    // Please replace each field in TRTCParams with your own parameters
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppID
    trtcParams.roomId = 123321; // Please replace with your own room number
    trtcParams.userId = @"denny"; // Please replace with your own userid
    trtcParams.userSig = @""; // Please replace with your own userSig
    trtcParams.role = TRTCRoleAnchor;
    // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
    [self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    return YES;
    }
    
    // Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered
    - (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 {
    // Override point for customization after application launch.
    // ...Other codes
    // Please replace each field in TRTCParams with your own parameters
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppID
    trtcParams.roomId = 123321; // Please replace with your own room number
    trtcParams.userId = @"denny"; // Please replace with your own userid
    trtcParams.userSig = @""; // Please replace with your own userSig
    trtcParams.role = TRTCRoleAudience;
    // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
    [self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    return YES;
    }
    
    // Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered
    - (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.
    // Initialize localCameraVideoView
    self.localCameraVideoView = [[UIView alloc] initWithFrame:self.window.bounds];
    self.localCameraVideoView.backgroundColor = [UIColor blackColor];
    [self.window addSubview:self.localCameraVideoView];
    
    // Set the preview mode of the local screen
    TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
    trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
    trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
    [self.trtcCloud setLocalRenderParams:trtcRenderParams];
    
    // Start a preview of the local camera
    [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.
    // Enable auto focus using TXDeviceManager
    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; // Add the localCameraVideoView property
    
    @end
    4. Initialize the localCameraVideoView in ViewController.m file.
    #import "ViewController.h"
    #import "AppDelegate.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // Initialize the localCameraVideoView
    self.localCameraVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.localCameraVideoView.backgroundColor = [UIColor blackColor];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Get the AppDelegate instance
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    // Add the localCameraVideoView to the trtcCloud of the AppDelegate
    [appDelegate.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];
    // Add the localCameraVideoView to the view of the ViewController
    [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.
    // Enable microphone acquisition and set the current scene to: Voice mode
    // For high noise suppression capability, strong and weak network resistance
    [self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
    
    // Enable microphone acquisition, and set the current scene to: Music mode
    // For high fidelity acquisition, low sound quality loss, recommended to use with professional sound cards
    [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.
    // Play denny's camera footage
    [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.
    // Stop denny's camera footage
    [self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
    // Stop all camera footages
    [self.trtcCloud stopAllRemoteView];

    Step 9. Play/stop the audio stream

    Mute the voice of remote user denny by calling the muteRemoteAudio("denny", true).
    // Mute user with id denny
    [self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
    You can also unmute him later by calling the muteRemoteAudio("denny", false).
    // Unmute user with id denny
    [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.
    // Exit current room
    [self.trtcCloud exitRoom];
    
    // Listen for the onExitRoom callback to find out why you checked out
    - (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

    API Reference at API Reference.
    If you encounter any issues during integration and use, please refer to Frequently Asked Questions.

    Contact us

    If you have any suggestions or feedback, please contact info_rtc@tencent.com.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support