Pre-call Detection
Function Overview
To ensure a real-time call experience, devices can be detected before the call to identify and troubleshoot problems in advance.
Device detection mainly detects whether the local microphone, camera, and speaker can work normally.
This article will introduce how to use ZEGO SDK interfaces to implement device detection.
This feature is not supported in WebGL environments.
Prerequisites
Before implementing pre-call device detection functionality, please ensure:
- You have created a project in ZEGOCLOUD Console and applied for valid AppID and AppSign. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in the project and implemented basic audio and video streaming functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
Device Detection
Microphone Detection
Detection Logic
The microphone device detection flow 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 will automatically check microphone permissions. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permissions in system settings.
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 Audio Data"), and the microphone audio data detection is normal, then 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
OnDeviceErrorcallback to detect whether the device is abnormal.
/**
* Local device exception notification.
*
* Supported versions: 2.15.0 and later.
* Description: Local device exception.
* Trigger 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 delegate void OnLocalDeviceExceptionOccurred(ZegoDeviceExceptionType exceptionType,ZegoDeviceType deviceType,string deviceID);4. Detect Microphone Audio Data
Call the StartSoundLevelMonitor interface to get the energy value of the sound collected by the microphone. If the data is normal, the microphone is working properly and can be used for calls.
engine.StartSoundLevelMonitor();Camera Detection
Only when accessing real-time audio SDK or pure audio scenarios, camera detection is not required and this section can be ignored.
Detection Logic
The camera device detection flow is shown in the following figure:
Corresponding Interfaces
1. Start Camera
Call the StartPreview interface to bind the view of the camera preview (for details, please refer to Enable Local Preview), and start video capture and preview without publishing stream.
engine.StartPreview();2. Detect Camera Permission
ZEGO SDK will automatically check camera permissions. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permissions in system settings.
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 Video is Normal"), and the video display is normal, then 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
OnDeviceErrorcallback to detect whether the device is abnormal.
/**
* Local device exception notification.
*
* Supported versions: 2.15.0 and later.
* Description: Local device exception.
* Trigger 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 delegate void OnLocalDeviceExceptionOccurred(ZegoDeviceExceptionType exceptionType,ZegoDeviceType deviceType,string deviceID);4. Detect Whether Video is Normal
If the video display is normal at this time, the camera is working properly and can be used for calls.
Speaker Detection
Detection Logic
The playback device detection flow is shown in the following figure:
Corresponding Interfaces
1. Use Audio Effect Player to Play Audio File
Call the ZegoAudioEffectPlayer interface to play your audio file used for testing.
// 1. Create audio effect player
ZegoAudioEffectPlayer audioEffectPlayer= engine.CreateAudioEffectPlayer();
// 2. Load resource
string currentSelectPath = "xxx";
audioEffectPlayer.LoadResource(1, currentSelectPath, (errorCode) =>
{
Debug.Log("LoadResource errorCode:" + errorCode);
});
// 3. Play resource
audioEffectPlayer.Start(1, currentSelectPath, null);2. Detect Whether Sound is Heard
If you can hear the corresponding audio, the playback device is working properly and can be used for calls. Call the OnAudioEffectPlayStateUpdate callback to view the audio effect player status:
/**
* Audio effect playback state callback.
*
* Supported versions: 1.16.0 and later.
* Description: This callback is triggered when the playback state of an audio effect in the audio effect player changes.
* Trigger timing: This callback is triggered when the playback state of an audio effect changes.
* Usage restrictions: None.
*
* @param audioEffectPlayer The audio effect player instance that triggered this callback.
* @param audioEffectID The ID of the audio effect resource that triggered this callback.
* @param state The playback state of the audio effect.
* @param errorCode Error code, for details please refer to common error code document https://www.zegocloud.com/docs/real-time-video-android-java/client-sdk/error-code.html .
*/
public delegate void OnAudioEffectPlayStateUpdate(ZegoAudioEffectPlayer audioEffectPlayer, uint audioEffectID, ZegoAudioEffectPlayState state, int errorCode);