Skip to main content
Android
iOS
macOS
Web
Windows
Electron
Flutter
React Native
React JS
Unity
Unreal Engine
Unreal (Blueprint)

Virtual Background

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-pictureportrait-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.

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.

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

Set a blurred background

To blur the video background, use the following code:

void UMyUserWidget::SetBackgroundBlur(VirtualBackgroundSource& virtualBackgroundSource)
{
VirtualBackgroundSource virtualBackgroundSource;
SegmentationProperty segmentationProperty;

// Set background blur
virtualBackgroundSource.background_source_type = VirtualBackgroundSource::BACKGROUND_BLUR;
virtualBackgroundSource.blur_degree = VirtualBackgroundSource::BACKGROUND_BLUR_DEGREE::BLUR_DEGREE_HIGH;

// Set processing properties for background
segmentationProperty.modelType = SegmentationProperty::SEG_MODEL_AI;
segmentationProperty.greenCapacity = 0.5F;

// Disable virtual background
agoraEngine->enableVirtualBackground(false, virtualBackgroundSource, segmentationProperty);
UE_LOG(LogTemp, Log, TEXT("Color background enabled"));

UE_LOG(LogTemp, Log, TEXT("Blur background enabled"));
}
Copy

Set a color background

To apply a solid color as the virtual background, use a hexadecimal color code. For example, 0x0000FF for blue:

void UMyUserWidget::SetBackgroundColor(VirtualBackgroundSource& virtualBackgroundSource)
{
VirtualBackgroundSource virtualBackgroundSource;
SegmentationProperty segmentationProperty;

// Set a solid background color
virtualBackgroundSource.background_source_type = VirtualBackgroundSource::BACKGROUND_COLOR;
virtualBackgroundSource.color = 0x0000FF;

// Set processing properties for background
segmentationProperty.modelType = SegmentationProperty::SEG_MODEL_AI;
segmentationProperty.greenCapacity = 0.5F;

// Disable virtual background
agoraEngine->enableVirtualBackground(false, virtualBackgroundSource, segmentationProperty);
UE_LOG(LogTemp, Log, TEXT("Color background enabled"));
}
Copy

Set an image background

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

void UMyUserWidget::SetBackgroundImage(VirtualBackgroundSource& virtualBackgroundSource)
{
VirtualBackgroundSource virtualBackgroundSource;
SegmentationProperty segmentationProperty;

// Set a background image
virtualBackgroundSource.background_source_type = VirtualBackgroundSource::BACKGROUND_IMG;
virtualBackgroundSource.source = "<absolute path to an image file>";

// Set processing properties for background
segmentationProperty.modelType = SegmentationProperty::SEG_MODEL_AI;
segmentationProperty.greenCapacity = 0.5F;

// Disable virtual background
agoraEngine->enableVirtualBackground(false, virtualBackgroundSource, segmentationProperty);
UE_LOG(LogTemp, Log, TEXT("Image background enabled"));
}
Copy

Reset the background

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

void UMyUserWidget::ResetVirtualBackground()
{
// Set a default or reset state for the virtual background
VirtualBackgroundSource virtualBackgroundSource;
SegmentationProperty segmentationProperty;

// For example, setting a default background color
virtualBackgroundSource.background_source_type = VirtualBackgroundSource::BACKGROUND_COLOR;
virtualBackgroundSource.color = 0xFFFFFF;

// Set processing properties for background
segmentationProperty.modelType = SegmentationProperty::SEG_MODEL_AI;
segmentationProperty.greenCapacity = 0.5F;

// Disable virtual background
agoraEngine->enableVirtualBackground(false, virtualBackgroundSource, segmentationProperty);
}
Copy

Reference

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

Video Calling