Voice Call
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • React Native
  • Electron
  • Unity3D
  • Cocos Creator
  • Windows
  • macOS
  • Linux
  • Overview
  • Develop your app
    • Integrate the SDK
    • Implement a basic voice call
    • Enhance basic feature
      • Use Tokens for authentication
      • Set up common audio config
      • Check the room connection status
  • Upgrade using advanced features
    • Advanced features
      • Configure the audio
        • Beautify & Change the voice
      • Improve audio quality
        • Test network and devices in advance
        • Monitor streaming quality
        • Visualize the sound level
      • Message signaling
        • Broadcast real-time messages to a room
        • Convey extra information using SEI
        • Quotas and limits
      • Play media files
        • Play media files
        • Play sound effects
      • Mix the streams
      • Record media data
      • Encrypt the streams
    • Distincitve features
      • Join multiple rooms
      • Customize the audio
      • Play streams via URL
      • Low-latency live streaming
  • Resources & Reference
    • SDK
    • Sample code
    • API reference
    • Debugging
    • FAQs
    • Key concepts
  • Documentation
  • Voice Call
  • Develop your app
  • Implement a basic voice call

Implement a basic voice call

Last updated:2023-05-29 11:55

Prerequisites

Before you begin, make sure you complete the following steps:

  1. Create a project in ZEGOCLOUD Admin Console, and get the AppID of your project.
  2. Integrate the ZEGO Express SDK into your project. For details, see Integration.

If the version of the ZEGO Express SDK you are using is under 2.17.0, to get the AppSign, contact the ZEGOCLOUD Technical Support. To upgrade the authentication mode from using the AppSign to Token, see Upgrade guide.

Implementation steps

The following diagram shows the API call sequence of the stream publishing and playing process:

apicallsequence_en

Create a ZegoExpressEngine instance

1. Optional: Create the UI

Add UI elements

Before creating a ZegoExpressEngine instance, we recommend you add the following UI elements to implement the basic real-time audio feature:

  • An audio window
  • An End button
layout

2. Import the header file

Import the header file ZegoExpressEngine.h into the project.

// Import the header file ZegoExpressEngine.h file.
#import <ZegoExpressEngine/ZegoExpressEngine.h>

3. Create a ZegoExpressEngine instance

To create a singleton instance of the ZegoExpressEngine class, call the createEngine method with the AppID of your project.

To receive callbacks, implement an event handler object that conforms to the ZegoEventHandler protocol(for example, self), and then pass the implemented event handler object to the createEngine method as the eventHandler parameter. Alternatively, you can pass nil to the createEngine method as the eventHandler parameter for now, and then call the method setEventHandler to set up the event handler after creating the engine.


ZegoEngineProfile *profile = [ZegoEngineProfile new];
// The AppID value you get from the ZEGO Admin console.
profile.appID = appID; 
// Use the general scenario.
profile.scenario = ZegoScenarioGeneral; 
// Create a ZegoExpressEngine instance and set eventHandler to [self]. If eventHandler is set to [nil], no callback will be received. You can set up the event handler later by calling the [-setEventHandler:] method.
[ZegoExpressEngine createEngineWithProfile:profile eventHandler:self];

Log in to a room

1. Log in

Before logging in to a room, you will need to generate a token first; Otherwise, the login will fail.
To generate a token, refer to the User privilege control.

To log in to a room, do the following:

  1. Create a ZegoUser with a unique user ID.
  2. Call the loginRoom method with the following parameters:
    • A unique room ID as the roomID parameter
    • The ZegoUser object created in the previous step as the user parameter

If the roomID does not exist, a new room will be created and you will log in automatically when you call the loginRoom method.

  • Each roomID must be globally unique within the scope of the AppID.
  • Each userID must be globally unique within the scope of the AppID. We recommend you set the userID to a meaningful value. You can associate userID with the account system of your application.
  • When you creating a ZegoUser object, the constructor method (userWithUserID) sets the userName property to the same value of the userID parameter you pass to the constructor method. The userID and userName properties cannot be nil. Otherwise, the room login will fail.
//create a user
ZegoUser *user = [ZegoUser userWithUserID:@"user1"];

ZegoRoomConfig *roomConfig = [[ZegoRoomConfig alloc] init];
// Token is generated by the user's own server. For an easier and convenient debugging, you can get a temporary token from the ZEGOCLOUD Admin Console
roomConfig.token = @"xxxxx";
// onRoomUserUpdate callback can be received only by passing in a ZegoRoomConfig whose "isUserStatusNotify" parameter value is "true".
roomConfig.isUserStatusNotify = YES;
// log in to a room
[[ZegoExpressEngine sharedEngine] loginRoom:roomID user:user config:roomConfig];

After you have successfully logged in to the room, if the app exits unexpectedly, you need to call the logoutRoom method to exit the room after restarting the app, and then call the loginRoom method to log in to the room again.

2. Listen for and handle the event callbacks related to room users and streams

To listen for and handle various events that may happen after logging in to a room, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to room users and streams:

  • onRoomStateUpdate: Callback for updates on current user's room connection status. When the current user's room connection status changes (for example, when the current user is disconnected from the room or login authentication fails), the SDK sends out the event notification through this callback.

  • onRoomUserUpdate: Callback for updates on the status of other users in the room. When other users join or leave the room, the SDK sends out the event notification through this callback.

  • onRoomStreamUpdate: Callback for updates on the status of the streams in the room. When new streams are published to the room or existing streams in the room stop, the SDK sends out the event notification through this callback.

  • To receive the onRoomUserUpdate callback, you must set the isUserStatusNotify property of the room configuration parameter ZegoRoomConfig to true when you call the loginRoom method to log in to a room.
  • To play streams published by other users in the room: you can listen for the onRoomStreamUpdate callback, and when there is a stream added, call the startPlayingStream method to start receiving and playing the newly added stream.
// Conform to the ZegoEventHandler protocol to handle event callbacks.
@interface ViewController () <ZegoEventHandler>
// ······
@end

@implementation ViewController

// Common event callbacks related to room users and streams.

// Callback for updates on the current user's room connection status.
- (void)onRoomStateUpdate:(ZegoRoomState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData roomID:(NSString *)roomID {
    // Implement the callback handling logic as needed. 
}

// Callback for updates on the status of other users in the room.
- (void)onRoomUserUpdate:(ZegoUpdateType)updateType userList:(NSArray<ZegoUser *> *)userList roomID:(NSString *)roomID {
    // Implement the callback handling logic as needed. 
}

// Callback for updates on the status of the streams in the room.
- (void)onRoomStreamUpdate:(ZegoUpdateType)updateType streamList:(NSArray<ZegoStream *> *)streamList extendedData:(nullable NSDictionary *)extendedData roomID:(NSString *)roomID {
    // Implement the callback handling logic as needed.
}

@end

Publish streams

1. Start publishing a stream

To start publishing a local audio stream to remote users, call the startPublishingStream method with the corresponding stream ID passed to the streamID parameter.

streamID must be globally unique within the scope of the AppID. If different streams are published with the same streamID, the ones that are published after the first one will fail.

// Start publishing a stream.
[[ZegoExpressEngine sharedEngine] startPublishingStream:@"stream1"];

2. Listen for and handle the event callbacks related to stream publishing

To listen for and handle various events that may happen after stream publishing starts, you can implement the corresponding event callback methods of the event handler as needed. The following is a common event callback related to stream publishing:

  • onPublisherStateUpdate: Callback for updates on stream publishing status. After stream publishing starts, if the status changes, (for example, when the stream publishing is interrupted due to network issues and the SDK retries to start publishing the stream again), the SDK sends out the event notification through this callback.
// Conform to the ZegoEventHandler protocol to handle event callbacks.
@interface ViewController () <ZegoEventHandler>
// ······
@end

@implementation ViewController

// Common event callbacks related to stream publishing.

// Callback for updates on stream publishing status.  
- (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData streamID:(NSString *)streamID {
    // Implement the callback handling logic as needed.
}

@end

Play streams

1. Start playing a stream

To start playing a remote audio stream, call the startPlayingStream method with the corresponding Stream ID passed to the streamID parameter.to the canvas parameter.

You can obtain the stream IDs of the streams published by other users in the room from the callback onRoomStreamUpdate.

// Start playing a stream.
[[ZegoExpressEngine sharedEngine] startPlayingStream:@"stream1"];

2. Listen for and handle the event callbacks related to stream playing

To listen for and handle various events that may happen after stream playing starts, you can implement the corresponding event callback methods of the event handler as needed. The following is a common event callback related to stream playing:

  • onPlayerStateUpdate: Callback for updates on stream playing status. After stream playing starts, if the status changes (for example, when the stream playing is interrupted due to network issues and the SDK retries to start playing the stream again), the SDK sends out the event notification through this callback.
// Conform to the ZegoEventHandler protocol to handle event callbacks.
@interface ViewController () <ZegoEventHandler>
// ······
@end

@implementation ViewController

// Common event callbacks related to stream playing.

// Callback for updates on stream playing status. 
- (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
    // Implement the callback handling logic as needed.
}

@end

Test out the voice call

We recommend you run your project on a real device. If your app runs successfully, you should hear the sound and see the video captured locally from your device.

To test out the real-time audio and video features, visit the ZEGO Express Web Demo, and enter the same AppID, Server and RoomID to join the same room. If it runs successfully, you should be able to view the video from both the local side and the remote side, and hear the sound from both sides as well.

In audio-only scenarios, no video will be captured and displayed.

Stop publishing and playing streams

1. Stop publishing a stream

To stop publishing a local audio stream to remote users, call the stopPublishingStream method.

// Stop publishing a stream.
[[ZegoExpressEngine sharedEngine] stopPublishingStream];

2. Stop playing a stream

To stop playing a remote audio stream, call the stopPlayingStream method with the corresponding stream ID passed to the streamID parameter.

// Stop playing a stream.
[[ZegoExpressEngine sharedEngine] stopPlayingStream:@"stream1"];

Log out from a room

To log out from a room, call the logoutRoom method with the corresponding room ID passed to the roomID parameter.

// Log out of a room
[[ZegoExpressEngine sharedEngine] logoutRoom:@"room1"];

Destroy the ZegoExpressEngine instance

To destroy the ZegoExpressEngine instance and release the resources it occupies, call the destroyEngine method.

  • If you want to receive a callback to make sure the hardware resources are released, pass in the value callback when destroying the ZegoExpressEngine instance. This callback can only be used to send out a notification when the destruction of the engine is completed. You can't use this callback method to release engine-related resources.
  • If you don't want to receive any callbacks, pass nil to destroyEngine instead.
[ZegoExpressEngine destroyEngine:nil];
Page Directory