Device Detection
Function Introduction
To ensure real-time communication experience, device detection can be performed before calls or live streaming to identify and troubleshoot problems in advance. Device detection mainly detects whether the local microphone, camera, and speaker can work normally.
Prerequisites
Before implementing device detection functions, please ensure:
-
A project has been created in ZEGOCLOUD Console, and a valid AppID and AppSign have been applied for. For details, please refer to Console - Project Information.
-
ZEGO Express SDK has been integrated into the project, and basic audio and video publish/play functions have been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation Process.
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 Permission
ZEGO SDK automatically checks microphone permissions.
Because Android 6.0 requires dynamic permission requests for some important permissions, you cannot just 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) {
requestPermissions(permissionNeeded, 101);
}
}3. Detect whether Microphone is Available
Detect whether the device is abnormal through the following callback. If no abnormal feedback is detected (you can synchronously start "4. Detect Microphone Sound Data"), and the microphone sound data detection is normal, the microphone device is available.
Listen to the onLocalDeviceExceptionOccurred callback to detect whether the device is abnormal.
/**
* Local device exception notification
*
* Supported version: 1.0.0 and above.
* Detailed description: Local device exception.
* Notification timing: This callback is triggered when local audio or video device functions malfunction.
*
* @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 Sound Data
Call the startSoundLevelMonitor interface to obtain the energy value of the sound captured by the microphone. If the data is normal, the microphone is normal and can be used for calls or live streaming.
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 screen view and start video capture and preview without publishing stream.
engine.startPreview();2. Detect Camera Permission
ZEGO SDK will automatically check camera permissions.
Because Android 6.0 requires dynamic permission requests for some important permissions, you cannot just 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) {
requestPermissions(permissionNeeded, 101);
}
}3. Detect whether Camera is Available
Detect whether the device is abnormal through the following callback. If no abnormal feedback is detected (you can synchronously start "4. Detect whether Screen is Normal"), and the screen display is normal, the device is available.
Listen to the onLocalDeviceExceptionOccurred callback to detect whether the device is abnormal.
/**
* Local device exception notification
*
* Supported version: 1.0.0 and above.
* Detailed description: Local device exception.
* Notification timing: This callback is triggered when local audio or video device functions malfunction.
*
* @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 or live streaming.
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 or live streaming. Call the onMediaPlayerStateUpdate interface to view player status callback:
/**
* Player playback status callback
* @param mediaPlayer Player instance of the callback
* @param state Player status
* @param errorCode Error code. For details, please refer to the common error codes document
*/
public void onMediaPlayerStateUpdate(ZegoMediaPlayer mediaPlayer, ZegoMediaPlayerState state, int errorCode){}API Reference List
| Method | Description |
|---|---|
| startPreview | Start local preview |
| onLocalDeviceExceptionOccurred | Local device exception notification |
| startSoundLevelMonitor | Start volume change monitoring |
| onMediaPlayerStateUpdate | Player playback status callback |
Common Error Codes
When the developer receives onLocalDeviceExceptionOccurred device callback not equal to 0, please refer to Common Error Codes for related error codes.
