logo
Video Call
On this page

Pre-Call Network/Device Detection

2024-01-02

Feature Overview

To ensure a real-time call experience, network and device detection can be performed before the call to identify and resolve issues in advance.

  • Network detection: Detect the network environment, which can be used to judge or predict whether the network environment is suitable for publishing/playing streams of specified bitrates.
  • Device detection: Detect whether the local microphone, camera, and speaker can work normally.

This article will introduce how to use ZEGO SDK interfaces to implement the above two aspects of detection.

Prerequisites

Before implementing pre-call network/device detection functionality, please ensure:

Network Detection

Please refer to Network and Performance for operations.

Device Detection

Microphone Detection

Detection Logic

The microphone device detection process is shown in the following figure:

Corresponding Interfaces

1. Start Microphone

Call the startPreview interface to start audio capture without publishing stream.

engine.startPreview();

2. Detect Microphone Permissions

ZEGO SDK automatically checks microphone permissions.

Warning

Because Android 6.0 requires dynamic permission requests for some important permissions, you cannot only apply for static permissions through the "AndroidMainfest.xml" file. Therefore, you also need to refer to executing the following code, where "requestPermissions" is a method of "Activity".

String[] permissionNeeded = {
    "android.permission.RECORD_AUDIO"};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, "android.permission.RECORD_AUDIO") != PackageManager.PERMISSION_GRANTED) {
        //101 is requestCode, can be any number greater than 0, will be passed through to the permission request result callback onRequestPermissionsResult
        requestPermissions(permissionNeeded, 101);
    }
}

3. Detect Whether Microphone is Available

Detect whether the device is abnormal through the following callbacks. If no abnormal feedback is detected (can start "4. Detect Microphone Recording Data" synchronously), and the microphone recording data detection is normal, the microphone device is available.

  • Version 2.15.0 and later: Listen to the onLocalDeviceExceptionOccurred callback to detect whether the device is abnormal.
  • Versions before 2.15.0: Listen to the onDeviceError callback to detect whether the device is abnormal.
/**
 * Local device exception notification.
 *
 * Supported versions: 2.15.0 and later.
 * Detailed description: Local device exception.
 * Notification timing: This callback is triggered when local audio or video device function is abnormal.
 *
 * @param exceptionType Device exception type.
 * @param deviceType Device type that is abnormal.
 * @param deviceID Device ID. Currently only supports desktop devices, used to identify specific devices; for mobile devices, this parameter will return an empty string.
 */
public void onLocalDeviceExceptionOccurred(ZegoDeviceExceptionType exceptionType, ZegoDeviceType deviceType, String deviceID){

}

4. Detect Microphone Recording Data

Call the startSoundLevelMonitor interface to obtain the energy value of the sound captured by the microphone. If the data is not abnormal, the microphone is normal and can be used for calls.

engine.startSoundLevelMonitor();

Camera Detection

Detection Logic

The camera device detection process is shown in the following figure:

Corresponding Interfaces

1. Start Camera

Call the startPreview interface to bind the camera preview view and start video capture and preview without publishing stream.

engine.startPreview();

2. Detect Camera Permissions

ZEGO SDK automatically checks camera permissions.

Warning

Because Android 6.0 requires dynamic permission requests for some important permissions, you cannot only apply for static permissions through the "AndroidMainfest.xml" file. Therefore, you also need to refer to executing the following code, where "requestPermissions" is a method of "Activity".

String[] permissionNeeded = {
    "android.permission.CAMERA"};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED) {
        //101 is requestCode, can be any number greater than 0, will be passed through to the permission request result callback onRequestPermissionsResult
        requestPermissions(permissionNeeded, 101);
    }
}

3. Detect Whether Camera is Available

Detect whether the device is abnormal through the following callbacks. If no abnormal feedback is detected (can start "4. Detect Whether Screen is Normal" synchronously), and the screen display is normal, the device is available.

  • Version 2.15.0 and later: Listen to the onLocalDeviceExceptionOccurred callback to detect whether the device is abnormal.
  • Versions before 2.15.0: Listen to the onDeviceError callback to detect whether the device is abnormal.
/**
* Local device exception notification.
*
* Supported versions: 2.15.0 and later.
* Detailed description: Local device exception.
* Notification timing: This callback is triggered when local audio or video device function is abnormal.
*
* @param exceptionType Device exception type.
* @param deviceType Device type that is abnormal.
* @param deviceID Device ID. Currently only supports desktop devices, used to identify specific devices; for mobile devices, this parameter will return an empty string.
*/
public void onLocalDeviceExceptionOccurred(ZegoDeviceExceptionType exceptionType, ZegoDeviceType deviceType, String deviceID){

}

4. Detect Whether Screen is Normal

If the screen display is normal at this time, the camera is normal and can be used for calls.

Speaker Detection

Detection Logic

The playback device detection process is shown in the following figure:

Corresponding Interfaces

1. Use Media Player to Play Audio File

Call the ZegoMediaPlayer interface to play the audio file you use for testing.

// 1. Create player object.
ZegoMediaPlayer mediaPlayer = engine.createMediaPlayer();
// 2. Load resource.
String resourcePath = "xxx";
mediaPlayer.loadResource(resourcePath, null);
// 3. Play resource.
mediaPlayer.start();

2. Detect Whether Sound is Heard

If you can hear the corresponding audio, the playback device is normal and can be used for calls. Call the onMediaPlayerStateUpdate callback to view the player status callback:

/**
* Player playback status callback.
* @param mediaPlayer The player instance of the callback.
* @param state Player status.
* @param errorCode Error code, for details please refer to the common error codes document: /real-time-video-android-java/client-sdk/error-code#7.
*/
public void onMediaPlayerStateUpdate(ZegoMediaPlayer mediaPlayer, ZegoMediaPlayerState state, int errorCode){}

Previous

Using Token Authentication

Next

Call Quality Monitoring