All Products
Search
Document Center

ApsaraVideo Live:Device detection before a call

Last Updated:Dec 03, 2025

This topic describes how to detect a device before a call.

Feature introduction

Alibaba Real-Time Communication (ARTC) SDK provides the device detection feature before a voice or video call. This feature allows you to detect the local key audio and video devices such as microphones, speakers, and cameras. You can identify potential device issues before the call, which helps avoid delays in troubleshooting issues because of unexpected device failures during the call and ensures a smooth, efficient, and stable call experience.

Sample code

Android: Android/ARTCExample/AdvancedUsage/src/main/java/com/aliyun/artc/api/advancedusage/PreJoinChannelTest/PreJoinChannelTestActivity.java

iOS: iOS/ARTCExample/AdvancedUsage/PreJoinChannelTest/PreJoinChannelTestVC.swift

Implementation

1. Detect a microphone

Turn on the microphone. If no exceptions are detected on the microphone and the volume of collected audio is considered normal, the microphone can be used as expected.

1. Turn on the microphone

You can call the startAudioCaptureTest operation to enable audio collection from the microphone. The following describes the details.

Android
/**
 * @brief Enable audio collection from the microphone before a voice or video call.
 * @details You can call this operation to enable audio collection from the microphone. 
 * - Check whether audio can be collected as expected based on {@link AliRtcAudioVolumeObserver::onTestAudioVolumeCallback}.
 * @return 
 * - 0: The request is successful.
 * - Values other than 0: An error code is returned.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
public abstract int startAudioCaptureTest();
IOS
/**
 * @brief Enable audio collection from the microphone before a voice or video call.
 * @details You can call this operation to enable audio collection from the microphone.
 * - Check whether audio can be collected as expected based on {@link AliRtcAudioVolumeObserver::onTestAudioVolumeCallback}.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
- (void)startAudioCaptureTest;
Windows
/**
 * @brief Enable audio collection from the microphone before a voice or video call.
 * @details You can call this operation to enable audio collection from the microphone.
 * - Check whether audio can be collected as expected based on {@link AliRtcAudioVolumeObserver::onTestAudioVolumeCallback}.
 * @return
 * - 0: The request is successful.
 * - Values other than 0: An error code is returned.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
virtual int StartAudioCaptureTest() = 0;

2. Detect whether the microphone is abnormal

You can configure a callback to detect whether the microphone is abnormal. If no exceptions are detected on the microphone and the volume of collected audio is considered normal, the microphone can be used as expected. The following section describes the details about the callback.

Android
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceType}
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceExceptionType}
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
public void OnLocalDeviceException(AliRtcEngine.AliRtcEngineLocalDeviceType deviceType, AliRtcEngine.AliRtcEngineLocalDeviceExceptionType exceptionType, String msg){}
IOS
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcLocalDeviceType}
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcLocalDeviceExceptionType}
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
- (void)onLocalDeviceException:(AliRtcLocalDeviceType)deviceType exceptionType:(AliRtcLocalDeviceExceptionType)exceptionType message:(NSString *_Nullable)msg;
Windows
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliEngineLocalDeviceType}
 * @param exceptionType The type of the device exception. For more information, see {@link AliEngineLocalDeviceExceptionType}
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
virtual void OnLocalDeviceException(AliEngineLocalDeviceType deviceType, AliEngineLocalDeviceExceptionType exceptionType, const char* msg){};

3. Detect the volume of collected audio

You can configure a callback to detect the volume of collected audio. If no exceptions are detected, the microphone is considered normal and can be used as expected. The following section describes the details about the callback.

Android
/**
 * @brief The callback is invoked to detect the volume of collected audio before a voice or video call.  
 * @param volume
 */
public void OnTestAudioVolume(int volume) {}
IOS
/**
 * @brief The callback is invoked to detect the volume of collected audio before a voice or video call.
 * @param volume
 */
- (void)onTestAudioVolumeCallback:(int)volume;
Windows
/**
 * @brief The callback is invoked to detect the volume of collected audio before a voice or video call.
 * @param volume
 */
virtual void OnTestAudioVolumeCallback(int volumn) {}

4. Stop the microphone detection

You can call the stopAudioCaptureTest operation to stop the microphone volume detection after you detect whether the microphone is abnormal.

Android
/**
 * @brief Stop the volume detection of collected audio.
 * @return 
 * - 0: The request is successful.
 * - Values other than 0: An error code is returned.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
public abstract int stopAudioCaptureTest();
IOS
/**
 * @brief Stop the volume detection of collected audio.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
- (void)stopAudioCaptureTest;
Windows
/**
 * @brief Stop the volume detection of collected audio.
 * @return
 * - 0: The request is successful.
 * - Values other than 0: An error code is returned.
 * @note You can call this operation only before joining the call {@link JoinChannel}.
 */
virtual int StopAudioCaptureTest() = 0;

2. Detect a speaker

Play an audio file. If you can hear the corresponding audio, the speaker can be used as expected.

1. Play an audio file

You can call the playAudioFileTest operation to play an audio file for testing. If you can hear the corresponding audio, the speaker can be used as expected.

Android
/**
 * @brief Play the audio file.
 * @param filePath The path of the file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
public abstract int playAudioFileTest(String filePath) ;
IOS
/**
 * @brief Play the audio file.
 * @param filePath The path of the file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
- (int)playAudioFileTest:(NSString *_Nonnull)filePath;
Windows
/**
 * @brief Play the audio file.
 * @param filePath The path of the file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link JoinChannel}.
 */
virtual int PlayAudioFileTest(const char* filePath) = 0;

2. Detect whether the speaker is abnormal

You can configure a callback to detect whether the speaker is abnormal. If no exceptions are detected on the speaker and you can hear the corresponding audio, the speaker can be used as expected. The following section describes the details about the callback.

Android
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
public void OnLocalDeviceException(AliRtcEngine.AliRtcEngineLocalDeviceType deviceType, AliRtcEngine.AliRtcEngineLocalDeviceExceptionType exceptionType, String msg){}
IOS
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
- (void)onLocalDeviceException:(AliRtcLocalDeviceType)deviceType exceptionType:(AliRtcLocalDeviceExceptionType)exceptionType message:(NSString *_Nullable)msg;
Windows
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliEngineLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliEngineLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
virtual void OnLocalDeviceException(AliEngineLocalDeviceType deviceType, AliEngineLocalDeviceExceptionType exceptionType, const char* msg){};

3. Stop playing audio file

You can call the stopAudioFileTest operation to stop playing the audio file after you detect whether the speaker is abnormal.

Android
/**
 * @brief Stop playing the audio file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
public abstract int stopAudioFileTest();
IOS
/**
 * @brief Stop playing the audio file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link joinChannel}.
 */
- (int)stopAudioFileTest;
Windows
/**
 * @brief Stop playing the audio file.
 * @return
 * - =0: The request is successful.
 * - <0: The request fails.
 * @note You can call this operation only before joining the call {@link JoinChannel}.
 */
virtual int StopAudioFileTest() = 0;

3. Detect a camera

Set the camera view for local preview and start the camera preview. If no callbacks are invoked because of errors and the camera view is displayed as expected, the camera can be used as expected.

1. Set the view for local preview

You can call the setLocalViewConfig operation to set the camera view for local preview before starting the camera preview.

Android
/**
 * @brief Set the view for local preview.
 * @param viewConfig The display properties for the local video. For more information, see {@link com::alivc::rtc::AliRtcEngine::AliRtcVideoCanvas} for details
 * @param track  Only {@link AliRtcVideoTrack#AliRtcVideoTrackCamera} and {@link AliRtcVideoTrack#AliRtcVideoTrackScreen} are supported.
 * @return
 * - 0: The request is successful.
 * - < 0: The request fails.
 * @note You can switch a view before and after you call the joinChannel method. If you set canvas or {@link AliRtcVideoCanvas#view} to null, the camera stop displaying the view.
 *       If you want to reconfigure {@link AliRtcVideoCanvas#renderMode}, keep other properties in the canvas unchanged and modify only
 *       {@link AliRtcVideoCanvas#renderMode}.
 *       If you want to reconfigure {@link AliRtcVideoCanvas#mirrorMode}, keep other properties in the canvas unchanged and modify only
 *       {@link AliRtcVideoCanvas#mirrorMode}.
 */
public abstract int setLocalViewConfig(AliRtcVideoCanvas viewConfig, AliRtcVideoTrack track);
IOS
/**
 * @brief Set the view for local preview.
 * @param viewConfig The display properties for the local video. For more information, see {@link AliVideoCanvas} for details
 * @param track  Only {@link AliRtcVideoTrackCamera} and {@link AliRtcVideoTrackScreen} are supported.
 * @return
 * - 0: The request is successful.
 * - < 0: The request fails.
 * @note You can switch a view before and after you call the joinChannel method. If you set canvas or {@link AliVideoCanvas::view} to null, the camera stop displaying the view.
 *       If you want to reconfigure {@link AliVideoCanvas::renderMode}, keep other properties in the canvas unchanged and modify only
 *       {@link AliVideoCanvas::renderMode}.
 *       If you want to reconfigure {@link AliVideoCanvas::mirrorMode}, keep other properties in the canvas unchanged and modify only
 *       {@link AliVideoCanvas::mirrorMode}.
 */
- (int)setLocalViewConfig:(AliVideoCanvas *_Nullable)viewConfig forTrack:(AliRtcVideoTrack)track;
Windows
/**
 * @brief Set the view for local preview.
 * @param renderConfig The display properties for the local video. For more information, see {@link AliEngineVideoCanvas} for details
 * @param track  Only {@link AliEngineVideoTrackCamera} and {@link AliEngineVideoTrackScreen} are supported.
 * @return
 * - 0: The request is successful.
 * - Values other than 0: The request fails.
 * @note You can switch a view before and after you call the joinChannel method. If you set canvas or {@link AliEngineVideoCanvas::displayView} to null, the camera stop displaying the view.
 *       If you want to reconfigure {@link AliEngineVideoCanvas::renderMode}, keep other properties in renderConfig unchanged and only modify
 *       {@link AliEngineVideoCanvas::renderMode}.
 *       If you want to reconfigure {@link AliEngineVideoCanvas::mirrorMode}, keep other properties in renderConfig unchanged and only modify
 *       {@link AliEngineVideoCanvas::mirrorMode}.
 */
virtual int SetLocalViewConfig(AliEngineVideoCanvas renderConfig,
AliEngineVideoTrack track) = 0;

2. Start the camera preview

You can call the startPreview operation to start the camera preview.

Android
/**
 * @brief Start the local preview.
 * @details Start the local preview.. The camera is automatically turned on.
 * @return
 * - 0: The request is successful.
 * - < 0: The request fails.
 * @note If you do not call the {@link #setLocalViewConfig} operation to set the camera view, the preview fails. Stream ingest is not affected.
 * If you start the local preview before {@link #joinChannel}, the camera is automatically turned on.
 */
public abstract int startPreview();
IOS
/**
 * @brief Start the local preview.
 * @return
 * - 0: The request is successful.
 * - <0: The request fails.
 * @note If you do not call the {@link setLocalViewConfig:forTrack:} operation to set the camera view, the preview fails. Stream ingest is not affected.
 * If you start the local preview before {@link joinChannel:name:onResultWithUserId:}, the camera is automatically turned on.
 */
- (int)startPreview;
Windows
/**
 * @brief Start the local preview.
 * @return
 * - 0: The request is successful.
 * - Values other than 0: The request fails.
 * @note If you do not call the {@link SetLocalViewConfig} operation to set the camera view, the preview fails. Stream ingest is not affected.
 *       If you start the local preview before {@link JoinChannel}, the camera is automatically turned on.
 */
virtual int StartPreview(int width = 0, int height = 0) = 0;

3. Detect whether the camera is abnormal

You can configure a callback to detect whether the camera is abnormal. If no exceptions are detected on the camera and the camera view is displayed as expected, the camera can be used as expected. The following section describes the details about the callback.

Android
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcEngine.AliRtcEngineLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
public void OnLocalDeviceException(AliRtcEngine.AliRtcEngineLocalDeviceType deviceType, AliRtcEngine.AliRtcEngineLocalDeviceExceptionType exceptionType, String msg){}
IOS
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliRtcLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliRtcLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
- (void)onLocalDeviceException:(AliRtcLocalDeviceType)deviceType exceptionType:(AliRtcLocalDeviceExceptionType)exceptionType message:(NSString *_Nullable)msg;
Windows
/**
 * @brief The callback is invoked when a local device exception occurs.
 * @param deviceType The type of the device. For more information, see {@link AliEngineLocalDeviceType}.
 * @param exceptionType The type of the device exception. For more information, see {@link AliEngineLocalDeviceExceptionType}.
 * @param msg The information carried in the exception.
 * @note This callback identifies a device exception that cannot be resolved. After you receive the callback, check whether the device is available.
 */
virtual void OnLocalDeviceException(AliEngineLocalDeviceType deviceType, AliEngineLocalDeviceExceptionType exceptionType, const char* msg){};