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.

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.

const checkCompatibility = () => {
if (!extension.current.checkCompatibility()) {
console.error("Does not support virtual background!");
return;
}
}
Copy

Configure the virtual background extension

Initializes and configures the virtual background processor, ensuring compatibility and handling initialization errors.

useEffect(() => {
const initializeVirtualBackgroundProcessor = async () => {
AgoraRTC.registerExtensions([extension.current]);
checkCompatibility();
console.log("Initializing virtual background processor...");
try {
processor.current = extension.current.createProcessor();
await processor.current.init(wasm);
localCameraTrack.pipe(processor.current).pipe(agoraContext.localCameraTrack.processorDestination);
processor.current.setOptions({ type: "color", color: "#00ff00" });
await processor.current.enable();
} catch (error) {
console.error("Error initializing virtual background:", error);
}
};

void initializeVirtualBackgroundProcessor();

return () => {
const disableVirtualBackground = async () => {
processor.current?.unpipe();
localCameraTrack?.unpipe();
await processor.current?.disable();
};
void disableVirtualBackground();
};
}, [localCameraTrack]);
Copy

Set a blurred background

To blur the video background, use the following code:

const blurBackground = () => {
processor.current?.setOptions({ type: "blur", blurDegree: 2 });
};
Copy

Set a color background

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

const colorBackground = () => {
processor.current?.setOptions({ type: "color", color: "#00ff00" });
};
Copy

Set an image background

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

const imageBackground = () => {
const image = new Image();
image.onload = () => {
processor.current?.setOptions({ type: "img", source: image });
};
image.src = demoImage;
};
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.

Interactive Live Streaming