Manage audio and video devices

Updated at:

ApsaraVideo Real-time Communication (ARTC) SDK for Web allows you to manage audio and video devices, which involves the selection and control of user devices such as microphones, cameras, and speakers. Proper management of these devices can ensure that users have a good communication experience. This topic provides the sample code that is used to manage audio and video devices.

Manage video devices

Query the camera list

You can call the static method getCameraList to query the list of available cameras.

// Call the static method.
const cameraList = await AliRtcEngine.getCameraList();
cameraList.forEach((device) => {
  console.log(`camera label: ${device.label}, deviceId: ${device.deviceId}`);
});

Enable or disable the camera

If you do not need to use the camera, set the enableCamera property to false to disable the camera.

Note

After the camera is disabled, you cannot enable the camera by calling the leaveChannel method and then calling the joinChannel method. To enable the camera, you must set the enableCamera property to true.

// Make sure that an AliRtcEngine instance is created.
aliRtcEngine.enableCamera = false;

If you want to use the camera again, set the enableCamera property to true to enable the camera.

aliRtcEngine.enableCamera = true;

Switch the camera

If you need to switch the camera, call the switchCamera method and specify the device ID of the camera to which you want to switch.

// Make sure that an AliRtcEngine instance is created.
// You can obtain the device ID of the camera from the camera list.
const targetCameraId = cameraList[0].deviceId;
await aliRtcEngine.switchCamera(targetCameraId);

Query the current camera

If you want to query the camera that is currently in use, call the getCurrentCameraDeviceId method to obtain the device ID of the camera.

// Make sure that an AliRtcEngine instance is created.
// If no camera is being used, the value of currentCameraId and the value of currentCamera are undefined.
const currentCameraId = aliRtcEngine.getCurrentCameraDeviceId();
const currentCamera = cameraList.find((device) => device.deviceId === currentCameraId);

Send or stop sending local video data

If you want to stop sending local video data but keep collecting the data, call the muteLocalCamera method and set the relevant parameter to true. If you want to send local video data again, call the muteLocalCamera method and set the relevant parameter to false.

// Set the parameter to true when you do not want to send local video data.
aliRtcEngine.muteLocalCamera(true);
// Set the parameter to false when you want to send local video data.
aliRtcEngine.muteLocalCamera(false);

Manage audio devices

Query the microphone list

You can call the static method getMicrophoneList to query the list of available microphones.

// Call the static method.
const micList = await AliRtcEngine.getMicrophoneList();
micList.forEach((device) => {
  console.log(`microphone label: ${device.label}, deviceId: ${device.deviceId}`);
});

Enable or disable the microphone

If you do not need to use the microphone, call the publishLocalAudioStream interface to pass in false to disable.

Note

After the microphone is disabled, you cannot enable the microphone by calling the leaveChannel method and then calling the joinChannel method. To enable the microphone, you need to call the publishLocalAudioStream interface to pass in true to restart.

// Make sure that an AliRtcEngine instance is created.
aliRtcEngine.publishLocalAudioStream(false);

If you want to use the microphone again, need to call the publishLocalAudioStream interface and pass in true to re-enable it.

aliRtcEngine.publishLocalAudioStream(true);

Switch the microphone

If you need to switch the microphone, call the switchMicrophone method and specify the device ID of the microphone to which you want to switch.

// Make sure that an AliRtcEngine instance is created.
// You can obtain the device ID of the microphone from the microphone list.
const targetMicId = micList[0].deviceId;
await aliRtcEngine.switchMicrophone(targetMicId);

Query the current microphone

If you want to query the microphone that is currently in use, call the getCurrentMicDeviceId method to obtain the device ID of the microphone.

// Make sure that an AliRtcEngine instance is created.
// If no microphone is being used, the value of currentMicId and the value of currentMic are undefined.
const currentMicId = aliRtcEngine.getCurrentMicDeviceId();
const currentMic = micList.find((device) => device.deviceId === currentMicId);

Send or stop sending local audio data

If you want to stop sending local audio data but keep collecting the data, call the muteLocalMic method and set the relevant parameter to true. If you want to send local audio data again, call the muteLocalMic method and set the relevant parameter to false.

// Set the parameter to true when you do not want to send local audio data.
aliRtcEngine.muteLocalMic(true);
// Set the parameter to false when you want to send local audio data.
aliRtcEngine.muteLocalMic(false);

Listen to device exceptions

To handle exceptions such as the input device being unplugged, listen to the localDeviceException event. When an exception occurs, perform the handling actions based on the exception type, for example, switch to another available input device.

// Query the enumerated exception types from the static attributes.
const AliRtcEngineLocalDeviceExceptionType = AliRtcEngine.AliRtcEngineLocalDeviceExceptionType;
// Listen to the localDeviceException event, which allows you to determine the exception type and handle the exception accordingly.
aliRtcEngine.on('localDeviceException', async (deciveType, exceptionType, message) => {
  console.log(`Device type ${deciveType} encounters an ${exceptionType} exception, message: ${message || 'empty'}.`);
  
  if (exceptionType === AliRtcEngineLocalDeviceExceptionType.AliEngineLocalDeviceExceptionTypeMicInterrupt) {
    // If the microphone is interrupted, the microphone may be unplugged. You can query the microphone list and switch to another microphone.
    const micList = await AliRtcEngine.getMicrophoneList();
    aliRtcEngine.switchMicrophone(micList[0].deviceId);
  } else if (exceptionType === AliRtcEngineLocalDeviceExceptionType.AliEngineLocalDeviceExceptionTypeCameraInterrupt) {
    // If the camera is interrupted, the camera may be unplugged. You can query the camera list and switch to another camera.
    const cameraList = await AliRtcEngine.getCameraList();
    aliRtcEngine.switchCamera(cameraList[0].deviceId);
  }
});

Hot swapping of devices

You can handle device changes by listening to the deviceChange event in the browser.

navigator.mediaDevices.addEventListener('devicechange', async() => {
  // Query the device list after device changes are detected.
  const cameraList = await AliRtcEngine.getCameraList();
  const micList = await AliRtcEngine.getMicrophoneList();
});