Voice Calling
  • Overview
  • Demo App
  • Sample Codes
  • SDK Integration
  • Solution Implementation
  • API Documents
  • Error Codes
  • Documentation
  • Voice Calling
  • Solution Implementation
  • iOS

Solution Implementation Guide for iOS

Last updated:2022-03-22 13:06

1 Function Description

One of the typical real-time voice scenarios is that members in the same room have real-time voice conversations.

2 Flow Description

Taking real-time voice between 2 people as an example, the main process is as follows:

Caution:

In the above process, the real-time voice between two room members is used as an example. In fact, the Zego SDK supports real-time voice for multiple people. It is recommended that developers design as needed.

The detailed steps are as follows: 1.Initialize SDK: Only after the SDK is initialized can login and push-pull streaming operations be performed. 2.Set callbacks: including room status callback, push flow callback, pull flow callback. 3.Log in to the room. 4.Push audio stream: Push local audio in real time. 5.Pull audio stream: Play audio of other users in the room. 6.Exit the room: To exit the room, you need to stop pushing and pulling, and then log out of the room.

3 Code Description

3.1 Initialization SDK

// Introduce ZegoExpressEngine.h header file
#import <ZegoExpressEngine/ZegoExpressEngine.h>

// Follow the ZegoEventHandler protocol to handle event callbacks that you care about
@interface ViewController () <ZegoEventHandler>
// ······
@end

// Fill in appID and appSign
unsigned int appID = ;  // Please obtain through the official website registration, the format is: 1234567890
NSString *appSign = ;  //Please register through the official website, the format is: @"0123456789012345678901234567890123456789012345678901234567890123" (total 64 characters)

// Create an engine, use a test environment, access to common scenarios, and register self as the eventHandler callback proxy
// If you do not need to register a callback, the eventHandler parameter can be passed nil, and you can call the "-setEventHandler:" method to set the callback proxy
[ZegoExpressEngine createEngineWithAppID:appID appSign:appSign isTestEnv:YES scenario:ZegoScenarioGeneral eventHandler:self];

Quick Starts - Implementation

3.2 Set Callbacks

To set the callback interface, you need to monitor and process the room status callback interface, push stream callback interface, and pull stream callback interface.

// Follow the ZegoEventHandler protocol to handle event callbacks that you care about
@interface ViewController () <ZegoEventHandler>
// ······
@end


@implementation ViewController

// ······

// The following are commonly used room related callbacks

- (void)onRoomStateUpdate:(ZegoRoomState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID {
    // Room status update callback. After logging into the room, when the room connection status changes (such as room disconnection, login authentication failure, etc.), the SDK will notify you through this callback
}

- (void)onRoomUserUpdate:(ZegoUpdateType)updateType userList:(NSArray<ZegoUser *> *)userList roomID:(NSString *)roomID {
    // The user status is updated. After logging in the room, when a user is added or deleted in the room, the SDK will notify you through this callback
}

- (void)onRoomStreamUpdate:(ZegoUpdateType)updateType streamList:(NSArray<ZegoStream *> *)streamList roomID:(NSString *)roomID {
    // The stream status is updated. After logging in to the room, when a user pushes or deletes an audio and video stream in the room, the SDK will notify you through this callback
}

// The following are commonly used push stream related callbacks

- (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
    // After the push streaming interface is successfully called, when the state of the push streamer changes, such as a network interruption causing abnormal push flow, the SDK will notify this callback when retrying the push flow
}

// The following are commonly used pull-related callbacks

- (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
    // After the pull stream interface is successfully called, when the state of the pull streamer changes, such as a network interruption causing abnormal flow pushing, the SDK will notify you through this callback while retrying the pull stream
}

// Implement other event callbacks as needed
@end

3.3 Logging In to A Room

Before conducting real-time voice conversations between users, they must first log in to the same room.

*Note: The room ID only supports strings with a maximum length of 128 bytes. The format only supports numbers, English characters and'~','!','@','#','$','%','^ ','&','','(',')','_','+','=','-','`',';',''',',', '.','<','>','/','' special characters.**

// Create User Object
ZegoUser *user = [ZegoUser userWithUserID:@"user1"];

// Start logging into the room
[[ZegoExpressEngine sharedEngine] loginRoom:@"room1" user:user];

Quick Starts - Implementation - 2.2

3.4 Start Pushing Audio Stream

After successfully logging into the room, you can push the audio stream. Developers can call startPublishingStream to push the stream. If you do not need to continue to push the stream, please call stopPublishingStream to stop the push.

*Note: streamID only supports character strings no longer than 256, and it needs to be globally unique within the entire AppID. If it appears in the same AppID, different users each pushed a stream and the stream name is the same, which will cause the user to push the stream back Push streaming failed. URL keywords cannot be included, otherwise the push-pull flow fails. The format only supports numbers, English characters and'~','!','@','#','$','%','^','&','','(',') ','_','+','=','-','`',';',''',',','.','<','>','/', '' Special characters.**

// Start pushing stream
[[ZegoExpressEngine sharedEngine] startPublishingStream:@"stream1"];

Quick Starts - Implementation - 2.3

3.5 Play Audio Stream

After successfully logging into the room, the live audio stream can be played. Developers can call startPlayingStream to pull the stream. If you do not need to continue to pull the stream, please call stopPlayingStream to stop the stream.

*Note: streamID only supports character strings up to 256 in length. URL keywords cannot be included, otherwise the push-pull flow fails. The format only supports numbers, English characters and'~','!','@','#','$','%','^','&','','(',') ','_','+','=','-','`',';',''',',','.','<','>','/', '' Special characters.**

// Start streaming, the stream name is obtained from onRoomStreamUpdate:(ZegoUpdateType)updateType streamList:(NSArray<ZegoStream *> *)streamList roomID:(NSString *)roomID callback after logging in the room, you will receive this when there are new/deleted streams Callback
[[ZegoExpressEngine sharedEngine] startPlayingStream:@"stream1"];

Quick Starts - Implementation - 2.4

3.6 End Audio Live Broadcast

After the audio call ends, the operations are mainly to log out of the room, stop pushing the stream, stop pulling the stream, clear the view or data, etc., and determine whether the SDK needs to be released according to the business needs.

// Stop pushing stream
[[ZegoExpressEngine sharedEngine] stopPublishingStream];
// Stop pulling stream
[[ZegoExpressEngine sharedEngine] stopPlayingStream:@"stream1"];
// Log out of the room
[[ZegoExpressEngine sharedEngine] logoutRoom:@"room1"];

// If you do not need to continue to use the SDK, you can call the following function to release the SDK. 
// Note: When the SDK needs to be used again after the SDK is released, the SDK must be re-initialized.
[ZegoExpressEngine destroyEngine:nil];
Page Directory