logo
On this page

Pre-call Detection

2024-01-02

Overview

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

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

This document will introduce how to use ZEGO Express SDK interfaces to implement the above two types of detection.

Network Detection

Please refer to Network Speed Test for operations.

Device Detection

Microphone Detection

Detection Logic

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

1. Start Microphone

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

ZegoExpressEngine.instance().startPreview();

2. Check Microphone Permission

ZEGO SDK automatically checks microphone permissions.

Warning

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

import {PermissionsAndroid} from 'react-native';

const granted = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA,
                                        PermissionsAndroid.RECORD_AUDIO);
granted.then((data)=>{

        if(!data) {
            const permissions = [PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, PermissionsAndroid.PERMISSIONS.CAMERA];
            PermissionsAndroid.requestMultiple(permissions);
        }
    }).catch((err)=>{
    console.log(err.toString());
    })
}

3. Check if Microphone is Available

Call the deviceError interface to detect whether the device is abnormal. If no abnormal feedback is detected (can start "4. Check Microphone Sound Data" synchronously), and the microphone sound data detection is normal, the microphone device is available.

/**
 * Audio/video device error notification
 * @param deviceName Device type name.
 * @param errorCode Error code, please refer to common error codes: /real-time-video-rn/error-code#7.
 */
deviceError: (errorCode: number, deviceName: string) => void;

4. Check Microphone Sound 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.

ZegoExpressEngine.instance().startSoundLevelMonitor();

Camera Detection

Detection Logic

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

1. Start Camera

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

render() {
    return (
    ...
    <ZegoTextureView ref='zego_preview_view' style={{height: 200}} />
    ...
    );
}

componentDidMount() {
    ...
    ZegoExpressEngine.instance().startPreview({"reactTag": findNodeHandle(this.refs.zego_preview_view), "viewMode": ZegoViewMode.AspectFit, "backgroundColor": 0});
    ...
}

2. Check Camera Permission

ZEGO SDK will automatically check camera permissions.

Warning

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

import {PermissionsAndroid} from 'react-native';

const granted = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA,
                                        PermissionsAndroid.RECORD_AUDIO);
granted.then((data)=>{

        if(!data) {
            const permissions = [PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, PermissionsAndroid.PERMISSIONS.CAMERA];
            PermissionsAndroid.requestMultiple(permissions);
        }
    }).catch((err)=>{
    console.log(err.toString());
    })
}

3. Check if Camera is Available

Call the deviceError interface to detect whether the device is abnormal. If no abnormal feedback is detected (can start "Check if Picture is Normal" synchronously), and the picture displays normally, the device is available.

/**
 * Audio/video device error notification
 * @param deviceName Device type name
 * @param errorCode Error code, please refer to common error codes: /real-time-video-rn/error-code#7
 */
deviceError: (errorCode: number, deviceName: string) => void;

4. Check if Picture is Normal

If the picture displays normally 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:

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
let mediaPlayer = ZegoExpressEngine.instance().createMediaPlayer();
// 2. Load resource
let resourcePath = "xxx";
mediaPlayer.loadResource(resourcePath);
// 3. Play resource
mediaPlayer.start();

2. Check if Sound is Heard

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

/**
 * 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 https://doc-en.zego.im/en/308.html
 */
mediaPlayerStateUpdate: (mediaPlayer: ZegoMediaPlayer, state: ZegoMediaPlayerState, errorCode: number) => void;

Previous

Use Token Authentication

Next

Call Quality Monitoring

On this page

Back to top