Talk to us
Talk to us
menu

How to Easily Build Voice Application with Voice API

How to Easily Build Voice Application with Voice API

As the world is transitioning to a new technology for real-time communication, voice calling is modernizing its existence with better features for voice API for developers to connect users anytime, anywhere. Voice APIs are stepping towards unified communications of all types of major industries.

This post is for those thriving developers to conduct detailed research using guidance on these real-time voice chat APIs to build their voice communications apps. Here are the best solutions for the developer to build a voice Application with the voice API of ZEGOCLOUD.

zegocloud voice api

What is Voice API?

The Voice API is a tool for software developers to make and receive calls with simple, easy-to-understand APIs. Behind the scenes, the voice API bridges public switched telephone networks (PSTNs) and Internet-connected applications. It allows programming voice calls to applications without specialized communication knowledge or hardware.

Using the voice API, developers can build calling logic that extends to any device, any user on any network, anywhere in the world. Developers can also add full VoIP functionality to their applications to make calls on the Internet alone, without the need to connect to PSTN.

Benefits of Voice call applications to build with Voice API

There are countless benefits to adding voice calling to your mobile or browser-based application.

  • Clear, fast communication

Voice communication minimizes the risk of misinterpretation. Even if you must go through a complicated procedure, you can easily explain it over the phone.

  • Cost-effectiveness

Voice calls apps significantly reduce domestic and international call rates. Unlike traditional phone solutions, voice chat apps do not require expensive equipment or maintenance.

  • Convenience

Voice calls provide a speedy and easy way to connect. Scrolling through the app’s contact list lets you easily find your contacts. Just click the button or touch the screen to start the connection. The high-speed Internet connection allows you to communicate instantly between domestic and international calls. Plus, you can make calls on various devices, including smartphones, tablets, laptops, and desktops, to make your business more efficient.

  • Audio Conference

In-app calls, unlike traditional calls, allow voice conferencing with 4-12 users, regardless of whether there is a media server. It is the best set-up for business meetings, medical discussions, financial consultations, HR interviews, and more when two or more people must participate in the call.

  • High Quality

A mature voice chat app has an excellent quality advantage compared to standard telephone services. Communication is clear and smooth, with interference, delay, or dropout issues.

Best Voice Chat API for Mobile and Web Apps 

All APIs may have unique features or offerings. However, developers must analyze the requirements to build Android, iOS & Web voice chats. The basic plan is to integrate a voice API that allows to development of a calling function that covers all types of users on any device, anywhere in the world, through many networks. Significant criteria to measure good Voice Chat API.

  • Popularity: A competent voice chat API gives developers programmatic control over calls and allows them to build call logic and VoIP functionality. However, the level of popularity and the number of users show the voice API’s efficiency.
  • Price: Affordability is also a key factor in distinguishing the best voice API providers. Live Voice call APIs can provide many features but are accessible by companies only if they are cost-effective.
  • Ease of use: Developers can integrate voice-calling APIs with CRM, help desk, apps, and many other mobile and web platforms, but this Integration must be easy and adaptable. Therefore, when considering and selecting the best voice call API for an app developer, it is necessary to ensure good usability.

ZEGOCLOUD API to Build Voice Application

ZEGOCLOUD is the best Voice Chat API for Mobile and Web Applications because of the major factors for the best voice chat API you can get here. It is very user-friendly.

Express Audio is a real-time audio, interactive service product that can provide developers convenient access, high-definition, smooth, multi-platform interoperability, low latency, high continuity audio and video services, enabling one-to-many, many-to-many real-time audio interactions, live shows of shows, audio conferences and other scenarios.

Features 

  • ZEGO Express SDK is a real-time audio and video SDK developed by ZEGO. You can quickly build high-quality, low-latency, and smooth real-time audio and video communications on various platform apps and support large-scale parallel processing.
  • Stream publishing: It offers the stream publishing process. The client app captures audio and video streams and sends them to ZEGO Real-Time Audio and Video Cloud.
  • Room: A service organizes room user groups and allows users in the same room to send and receive audio, video, and messages in real time.
  • If you want to publish or play a stream, log in to a room first.
  • It is essential to know one thing users can be notified only about modifications in the room they are in (new users joining rooms, existing users leaving spaces, new audio-video streams, etc.).

What can it do?

  • Social: For many people, text chat may feel unsatisfactory, video calls may be overwhelming, and audio conversations may be a good idea. In real-time voice chat, users tend to interact with each other longer.
  • In-game Audio: By adding real-time voice to the game, players can interact in real-time while playing together, creating a more impressive experience. As gamers engage more, so does their bottom line.
  • Voice conference: High-quality voice calls enable remote employees to collaborate effectively in real-time from anywhere.
  • IoT Devices: By adding real-time voice functionality to IoT devices, you can achieve a smarter and more convenient lifestyle.

ZEGO call solution also includes a demo calling app and complete open-source sample code to help you quickly build the calling app for the scenario you want in just a few minutes because it is straightforward.

Guides on Voice Call API for iOS Developers

Set up the development environment such as getting your laptop or PC in front of you, and ensure you have a good internet connection to get started smoothly.

Preparation

Before integrating the ZEGO Express SDK, make sure the development environment meets the following requirements:

  • React Native 0.60.0 or later
  • You must be an iOS device or iOS Simulator running on iOS 9.0 or later and supporting audio and video. We recommend you use an actual device.
  • Your device is connected to the internet.
  • Configure the VS Code environment. Search the “React Native Tools” in the application store to download it.

Optional: Create a new project

Skip to this step if a project already exists.

Import the SDK

Enter the root directory of your project, open the package.json, and add the “zego-express-engine-reactnative”: “^x.y.z” to the dependencies.

  • In the root directory of your project, run the npm install zego-express-engine-reactnative –save command, or the yarn add zego-express-engine-reactnative command to install the SDK.
  • Go to the iOS root directory, and run the pod installs command to install the dependencies.
  • Once you’ve done the operations above, you can use the zego-express-engine-reactnative SDK in your project in JavaScript or TypeScript (TypeScript is recommended).

Add permissions

Permissions can be set as needed.

In Xcode, select the target project, go to Info > Custom iOS Target Properties.

add permissions

Add the + button to add microphone and camera permissions.

  • Privacy – Camera Usage Description
  • Privacy – Microphone Usage Description

After the permission is added, it will be shown below:

device permissions

Creating a ZegoExpressEngine Instance:

1: User Interface: Creating UI as you like is not necessary. It is optional. User interface (UI) is how humans and computers interact and communicate on a device. It includes the display screen, keyboard, mouse, and desktop appearance. So you can adjust it before starting.

2: Import Header Files: To import the header, use the preprocessor directive #include to instruct the compiler to import and process the remaining code before compiling it. Import the header file ZegoExpressEngine.h into your project.

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

3: Create an instance of ZegoExpressEngine

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

To receive callbacks, implement an event handler object that conforms to the ZegoEventHandler protocol (for example, self) and pass the executed event handler object as an eventHandler parameter to the createEngine method. You can also set an event handler by passing nil as the eventHandler parameter to the createEngine method and then calling the setEventHandler method after the engine is created.

/**
*  appID: The AppID value you get from the ZEGOCLOUD Admin console.
*  The AppID ranges from 0 - 4294967295.
* appSign: The AppSign you get from the ZEGOCLOUD Admin console.
* scenario: the scenario you use. [ZegoScenarioGeneral] refers to the general scenario. 
*/
ZegoEngineProfile *profile = [[ZegoEngineProfile alloc] init];
profile.appID = <#appID#>;
profile.appSign = <#appSign#>;
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 into a room

1: Login

Note: You must generate a token before you can log in to the room. You must create a token before you can log in to the room.

To log in to a conference room:

  1. Create a ZegoUser with a unique user ID.
  2. Call the loginRoom the method with the given parameters:

The unique room ID as the roomID parameter

Use the ZegoUser object you created in the previous step as the user parameter.

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

//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 callback:^(int errorCode, NSDictionary * _Nullable extendedData) {
    // (Optional callback) The result of logging in to the room. If you only pay attention to the login result, you can use this callback.
}];

Suppose the app terminates abnormally after successfully logging in to a room. In that case, you must exit the room by calling the logoutRoom method after restarting the app, then log in to the room again by calling the loginRoom method.

2: Listen and process room users and stream-related event callbacks

If necessary, you can implement the event handlers’ corresponding event callback methods to listen and process the various events after logging in to the room. Below are the general event callbacks associated with room users and streams.

  • onRoomStateUpdate: A callback for updating the current user’s room connection status. If the current user’s room connection status changes (for example, if the present user is detached from the room or if login authentication fails), the SDK sends an event notification through this callback.
  • onRoomUserUpdate: A callback to update the status of other users in the room. When other users join or leave a room, the SDK sends event notifications via this callback.
  • onRoomStreamUpdate: A callback to update the status of a stream in a room. The SDK sends event notifications via this callback when a new stream is published to a room or when an existing stream in a room is stopped.
// 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.
// Users can only receive callbacks when the isUserStatusNotify property of ZegoRoomConfig is set to `true` when logging in to the room (loginRoom).
- (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. 
    // If users want to play the streams published by other users in the room, call the startPlayingStream method with the corresponding streamID obtained from the `streamList` parameter where ZegoUpdateType == ZegoUpdateTypeAdd.
}

@end

Publish Stream

Start publishing the stream.

To start publishing the local audio stream to a remote user, call the startPublishingStream method by passing the stream ID corresponding to the streamID parameter.

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

Audition and processing of event callbacks related to stream publishing

To listen to and process various events that may occur after stream publishing has started, you may implement the corresponding event callback methods of the event handlers as needed. Below are some standard event callbacks related to stream publishing.

  • onPublisherStateUpdate: Callback for updating the stream publishing status. If the status changes after a stream publishes has been started (for example, if a network problem is causing the stream to publish to stop and the SDK attempts to create the stream post again), the SDK sends an event notification via 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 Stream

Start playing the stream.

To start playing the remote audio stream, call the startPlayingStream method by passing the Stream ID corresponding to the streamID parameter to the canvas parameter.

Stream IDs for streams other users in the room have published can be obtained from the callback onRoomStreamUpdate.

// Play a remote stream
[[ZegoExpressEngine sharedEngine] startPlayingStream:@"stream1" canvas:[ZegoCanvas canvasWithView:self.view]];

Audition and processing of event callbacks related to stream playback

To wait for and process various events that may occur after the start of stream reproduction, it is necessary to implement an event callback method corresponding to an event handler. Below are the available event callbacks related to stream playback.

  • onPlayerStateUpdate: A callback for updating the stream’s playback status. If the status changes after the stream start playing (for example, if there is a network issue that interrupts the stream playback, and the SDK tries again to start the stream playback), the SDK will send an 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 Voice Call

It is recommended that you run the project on a real device. When the app runs successfully, you should hear audio and see locally captured video from your device.

To try out real-time audio and video, go to ZEGO Express Web Demo and join the same room with the same AppID, Server, and RoomID. If successful, you should be able to see video from both the local and remote sides and hear audio from both sides.

Guides on Voice Chat API for Android Developers

Build development environment

  • Android Studio 2.1 or later is required. Based on the speed and enhancements of Android Studio 2.0, the stable Android Studio 2.1 introduces updates to the IDE wizards, build systems, and Android Emulator to explore new features and APIs in the developer preview, including the new Jack compilers and Java 8 language support.
  • The Android SDK package: Android SDK 25、, Android SDK Build-Tools 25.0.2、, Android SDK Platform-Tools 25。
  • Android Device with camera and microphone running Android 4.4 or higher. You can also run the project on an Android emulator, but we recommend using a real machine. The Android emulator is an Android Virtual Device (AVD) representing a specific Android device. Using the Android emulator as a target platform, you can test Android applications on your PC. The use of the Android emulator is optional.
  • Your Android device and computer are linked to the Internet.

Include SDK in the project

Choose one of the following methods to incorporate ZEGO Express SDK into your project:

The SDK supports the Android ABI: armeabi-v7a, arm64-v8a, x86, x86_64.

Method 1: Integrate the SDK Automatically with JCenter

Gradle creates it effortless to build common types of projects, such as Java libraries, by adding layers of conventions and built features through plug-ins. You can also create and publish custom plug-ins that encapsulate your conventions and build functionality. Add the following line to the app/build.

...
dependencies {
    ...
    // x.y.z is the specific version number of the SDK, for example: 2.7.0
    // Latest version number can be get in the SDK release history.
    implementation 'com.github.zegolibrary:express-video:x.y.z'
}

Method 2: Download and Copy the SDK Files Manually

Download ZEGO Express Android SDK and unzip the downloaded SDK package. The Android SDK Build Tool is used to build the actual binaries of the Android app. The main functions of the Android SDK Build Tool are to develop, debug, run, and test Android applications.

Copy all folders containing the unzipped .jar and .so files to the project directory (for example, app/libs).

zegocloud voice api file

Add the Import statement for the SDK. Open the app/build.gradle file and add the following:

  1. Add an ndk node within the defaultConfig node and specify the supported APIs.
ndk {
  abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' 
}

2. Add the sourceSets node within the android node and specify the SDK files’ directory.

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

3. Add the following code to the dependencies node:

implementation fileTree(dir: 'libs', include: ['*.jar'])

Setting Permissions

Open app/src/main/AndroidManifest.xml and add the following code:

<!-- Permissions required by the SDK -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <!-- Permissons required by the App -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

Prevent Class Name Obfuscation

To prevent the Zego SDK public class names from being obfuscated, please add the following code in the file proguard-rules.pro.

-keep class **.zego.**{*;}

Create an instance of ZegoExpressEngine

1. Create UI as you like. Yser interface of your working environment.

2. Create ZegoExpressEngine Instance

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

To receive callbacks, implement an event handler object that conforms to the IZegoEventHandler protocol and pass the event handler object that you implemented as an eventHandler parameter to the createEngine method. Alternatively, you can set the event handler by calling the setEventHandler method after the engine is created by passing null as the eventHandler parameter to the createEngine method.

//  Create a ZegoExpressEngine instance and set eventHandler to [self]. 
// If eventHandler is set to [null], no callback will be received. You can set up the event handler later by calling the [setEventHandler:] method.
ZegoEngineProfile profile = new ZegoEngineProfile();
profile.appID = ;  // The AppID you get from ZEGOCLLOUD Admin Console. AppID format: 123456789L
profile.appSign = ;  // The AppSign you get from ZEGOCLOUD Admin Console. AppSign format: @"0123456789012345678901234567890123456789012345678901234567890123" (64 characters)
profile.scenario = ZegoScenario.GENERAL;  // Use general scenario.
profile.application = getApplication();
engine = ZegoExpressEngine.createEngine(profile, null);

Log into a room

1. Log in

To log in to a room, call the loginRoom method. If roomID does not exist, calling the loginRoom method creates a new room and automatically logs in.

/** create a user */
ZegoUser user = new ZegoUser("user1");


ZegoRoomConfig roomConfig = new ZegoRoomConfig();
/** 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 = "xxxx";
/** onRoomUserUpdate callback can be received only by passing in a ZegoRoomConfig whose "isUserStatusNotify" parameter value is "true".*/
roomConfig.isUserStatusNotify = true;

/** log in to a room */
engine.loginRoom("room1", user, roomConfig, (int error, JSONObject extendedData)->{
    // (Optional callback) The result of logging in to the room. If you only pay attention to the login result, you can use this callback.
});  

If the app closes abnormally after successfully logging in to a room, after restarting the app, you must close the room by calling the logoutRoom method and log in to the room by calling the loginRoom method again.

Listen and process room users and stream-related event callbacks

If you want to listen to and process the various events that may occur after you log in to a room, you can implement the corresponding event callback methods in the event handler. Below are the available event callbacks associated with room users and streams.

  • onRoomStateUpdate: A callback for updating the current user’s room connection status. If the current user’s room connection status changes (for example, if the current user is disconnected from the room or if login authentication fails), the SDK sends an event notification through this callback.
  • onRoomUserUpdate: A callback to update the status of other users in the room. When other users join or leave a room, the SDK sends event notifications via this callback.
  • onRoomStreamUpdate: A callback to update the status of a stream in a room. The SDK sends event notifications via this callback when a new stream is published to a room or when an existing stream in a room is stopped.
engine.setEventHandler(new IZegoEventHandler() {

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

    /** Callback for updates on the current user's room connection status. */
    @Override
    public void onRoomStateUpdate(String roomID, ZegoRoomState state, int errorCode, JSONObject extendedData) {
        /** Implement the callback handling logic as needed. */
    }

    /** Callback for updates on the status of other users in the room. */
    @Override
    public void onRoomUserUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoUser> userList) {
        /** Implement the callback handling logic as needed. */
    }

    /** Callback for updates on the status of the streams in the room. */
    @Override
    public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList, JSONObject extendedData){
        /** Implement the callback handling logic as needed. */
     }

});

Conclusion

Using powerful programmable voice APIs, you can build fast, reliable, and secure voice applications that connect, manage, and route calls across any device to create unlimited voice calls. Scale billions of concurrent user conversations across multiple platforms with a complete solution that guarantees high-end scalability with secure APIs and SDKs and increases user engagement and retention with high-quality voice calls.

APIs enable you to make, receive, and manage calls, creating a rich and reliable voice experience. To help us find the best API for a specific use case, we reviewed voice APIs and suggested ZEGOCLOUD is the best platform for developers. You can get everything that you expect as a developer. It is very user-friendly to finalize your project in a meager time.

Talk to Expert

Learn more about our solutions and get your question answered.

Talk to us

Take your apps to the next level with our video APIs

Free Trial
  • 10,000 minutes for free
  • 4,000+ corporate clients
  • 3 Billion daily call minutes

Stay updated with us by signing up for our newsletter!

Don't miss out on important news and updates from ZEGOCLOUD!

* You may unsubscribe at any time using the unsubscribe link in the digest email. See our privacy policy for more information.