logo
Live Streaming
On this page

Device Detection


Feature 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 checks whether the local microphone, camera, and speakers can work normally.

Prerequisites

Before implementing device detection, please make sure:

Usage Steps

Microphone Detection

Detection Logic

The microphone device detection process is shown below:

Corresponding Interfaces

1. Start Microphone

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

[[ZegoExpressEngine sharedEngine] startPreview:nil];

2. Check Microphone Permission

ZEGO SDK will automatically check microphone permission. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permission in system settings.

**3. Check if Microphone is Available

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

Listen to the onLocalDeviceExceptionOccurred callback to check whether the device is abnormal.

/// Local device exception notification
///
/// Supported since: 1.0.0 and above.
/// Description: Local device exception.
/// Trigger: This callback is triggered when the function of the local audio or video device is abnormal.
///
/// @param exceptionType Device exception type.
/// @param deviceType The type of device where the exception occurred.
/// @param deviceID Device ID. Currently, only desktop devices are supported to distinguish different devices; for mobile devices, this parameter will return an empty string.
- (void)onLocalDeviceExceptionOccurred:(ZegoDeviceExceptionType)exceptionType deviceType:(ZegoDeviceType)deviceType deviceID:(NSString *)deviceID;

4. Check Microphone Recording 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 normal and can be used for calls or live streaming.

[[ZegoExpressEngine sharedEngine] startSoundLevelMonitor];

Camera Detection

Detection Logic

The camera device detection process is shown below:

Corresponding Interfaces

1. Start Camera

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

ZegoCanvas *previewCanvas = [ZegoCanvas canvasWithView:self.previewView];
[[ZegoExpressEngine sharedEngine] startPreview:previewCanvas];

2. Check Camera Permission

ZEGO SDK will automatically check camera permission. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permission in system settings.

**3. Check if Camera is Available

Check whether the device is abnormal through the following callback. If no abnormal feedback is detected (you can start "4. Check if Screen is Normal" synchronously), and the screen display is normal, the device is available.

Listen to the onLocalDeviceExceptionOccurred callback to check whether the device is abnormal.

//// Local device exception notification
///
/// Supported since: 1.0.0 and above.
/// Description: Local device exception.
/// Trigger: This callback is triggered when the function of the local audio or video device is abnormal.
///
/// @param exceptionType Device exception type.
/// @param deviceType The type of device where the exception occurred.
/// @param deviceID Device ID. Currently, only desktop devices are supported to distinguish different devices; for mobile devices, this parameter will return an empty string.
- (void)onLocalDeviceExceptionOccurred:(ZegoDeviceExceptionType)exceptionType deviceType:(ZegoDeviceType)deviceType deviceID:(NSString *)deviceID;

4. Check if 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 below:

Corresponding Interfaces

1. Use Media Player to Play Audio File

Call the ZegoMediaPlayer interface to play your audio file for testing.

/// 1. Create media player
ZegoMediaPlayer *mediaPlayer = [[ZegoExpressEngine sharedEngine] createMediaPlayer];
/// 2. Load media resource
NSString *resourcePath = "xxx";
[mediaPlayer loadResource: resourcePath callback:^(int errorCode) {
    NSLog(@"Media Player load resource. errorCode: %d", errorCode);
}];
/// 3. Start playing media
[mediaPlayer start];

2. Check if 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 mediaPlayer:stateUpdate:errorCode: interface to check the media player status:

/// Player playing state callback
/// @param mediaPlayer The player instance of the callback
/// @param state Player state
/// @param errorCode Error code, please refer to the common error code document for details
- (void)mediaPlayer:(ZegoMediaPlayer *)mediaPlayer stateUpdate:(ZegoMediaPlayerState)state errorCode:(int)errorCode;

API Reference List

MethodDescription
startPreviewStart local preview
onLocalDeviceExceptionOccurredLocal device exception notification
startSoundLevelMonitorStart sound level monitoring
mediaPlayer:stateUpdate:errorCode:Player playing state 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.

Previous

Network Speed Test

Next

Multi-Source Capture