Virtual Background

Updated

Blur the background or replace it with a solid color or an image.

Virtual Background enables users to blur their background, or replace it with a solid color or an image. This feature is applicable to use-cases such as online conferences, online classes, and live streaming. It helps protect personal privacy and reduces audience distraction.

Understand the tech

Virtual Background offers the following options:

FeatureExample
Blurred background and image background
Video/Animated background
Portrait-in-picture Allows the presenter to use slides as the virtual background while superimposing their video. The effect is similar to a weather news cast on television, preventing interruptions during a layout toggle.
Together Mode Allows the presenter to segment the portrait in the remote video, transmit it locally, and customize the display.

Want to test Virtual Background? Try the online demo.

Prerequisites

Ensure that you have implemented the SDK quickstart in your project.

Implement virtual background

This section shows you how to add a virtual background to the local video.

Check device compatibility

To avoid performance degradation or unavailable features when enabling Virtual Background on low-end devices, check whether the device supports the feature.

guard agoraEngine.isFeatureAvailable(onDevice: .videoPreprocessVirtualBackground) else {
  // Device doesn't support virtual background
  return
}

Set a blurred background

To blur the video background, use the following code:

func blurBackground() {
  let virtualBackgroundSource = AgoraVirtualBackgroundSource()
  virtualBackgroundSource.backgroundSourceType = .blur
  virtualBackgroundSource.blurDegree = .high

  let segData = AgoraSegmentationProperty()
  segData.modelType = .agoraAi

  agoraEngine.enableVirtualBackground(true, backData: virtualBackgroundSource, segData: segData)
}

Set a color background

To apply a solid color as the virtual background, use a hexadecimal color code.

func colorBackground() {
  let virtualBackgroundSource = AgoraVirtualBackgroundSource()
  virtualBackgroundSource.backgroundSourceType = .color
  virtualBackgroundSource.color = convertColorToHex(.red)

  let segData = AgoraSegmentationProperty()
  segData.modelType = .agoraAi

  agoraEngine.enableVirtualBackground(true, backData: virtualBackgroundSource, segData: segData)
}

Set an image background

To set a custom image as the virtual background, specify the absolute path to the image file.

func imageBackground() {
  let virtualBackgroundSource = AgoraVirtualBackgroundSource()
  virtualBackgroundSource.backgroundSourceType = .img
  virtualBackgroundSource.source = Bundle.main.path(forResource: "background_ss", ofType: "jpg")

  let segData = AgoraSegmentationProperty()
  segData.modelType = .agoraAi

  agoraEngine.enableVirtualBackground(true, backData: virtualBackgroundSource, segData: segData)
}

Reset the background

To disable the virtual background and revert to the original video state, use the following code:

agoraEngine.enableVirtualBackground(false, backData: nil, segData: nil)

Reference

This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

API reference