Back to Blog

Use Virtual Backgrounds with the Agora React Native SDK

Use Virtual Backgrounds with the Agora React Native SDK

The Agora React Native SDK now supports using virtual backgrounds. You can use a solid color or pick an image for the background. We’ll take a look at how to bundle an image asset with React Native and how to select a user image to use as a virtual background.

Prerequisites

  • An Agora developer account (It’s free! Sign up here)
  • A basic understanding of React Native

To get us up to speed with a basic video calling app, let’s use this demo. You can follow the instructions in the readme to get the app working on your device. We’ll go over the code for using virtual backgrounds. You can find the source for this demo on GitHub.

We’ll need two open-source libraries: react-native-image-picker and react-native-fs. You can add them to the project by executing npm i --save react-native-image-picker react-native-fs

Using Virtual Backgrounds

this.virtualSource = new VirtualBackgroundSource({ // create the config
backgroundSourceType: VirtualBackgroundSourceType.Color,
color: new Color(1, 0, 0), //specify the RGB value
});
await this._engine?.enableVirtualBackground(true, this.virtualSource); // enable virtual background
view raw app.jsx hosted with ❤ by GitHub

The Agora SDK makes it really easy: you just call the enableVirtualBackground method on the engine object with your config to enable the feature. You don’t have to worry about things like segmentation or background removal.

We’ll define a property on our App class called virtualSource to create and store our config. We’ll add a waiting boolean to our state so that we can disable the start call button while the async code is being executed.

...
export default class App extends Component<Props, State> {
_engine?: RtcEngine;
virtualSource: VirtualBackgroundSource; // defining our config
constructor(props) {
super(props);
this.virtualSource = new VirtualBackgroundSource({ // initializing the config
backgroundSourceType: VirtualBackgroundSourceType.Color,
color: new Color(1, 1, 1),
})
this.state = {
appId: '<YourAgoraAppID>',
waiting: true, // add waiting boolean to the state
...
...
// create a function to set the config for using a color
useColor = () => {
let color = new Color(255, 0, 0);
this.virtualSource = new VirtualBackgroundSource({
backgroundSourceType: VirtualBackgroundSourceType.Color,
color: color,
});
this.setState({waiting: false});
}
...
view raw app.jsx hosted with ❤ by GitHub

We can define a function useColor, that uses the Color class constructor to create a color object using the passed red, green and blue values (color range is 0 — 255). We can then update the virtualSource property and our state.

Using Background Blur

useBlur = () => {
this.virtualSource = new VirtualBackgroundSource({
backgroundSourceType: VirtualBackgroundSourceType.Blur,
blur_degree: VirtualBackgroundBlurDegree.Medium
});
this.setState({waiting: false});
}
view raw app.tsx hosted with ❤ by GitHub

To bundle an image, you can copy it to the project directory and reference it using the required syntax. We can download the image using its URI while testing with RNFS. We can then update the source property of the config with the absolute path of the image.

Using a user image as the virtual background

...
pickImage = async () => {
this.setState({waiting: true});
await launchImageLibrary({ mediaType: 'photo' }, async (res) => {
if (res.assets) {
let uri = res.assets[0].uri;
if (uri) {
if(Platform.OS === 'android') {
await RNFS.copyFile(uri, `${RNFS.DocumentDirectoryPath}/img.png`);
} else if (Platform.OS === 'ios') {
await RNFS.downloadFile({ fromUrl: uri, toFile: `${RNFS.DocumentDirectoryPath}/img.png` })
}
this.virtualSource = new VirtualBackgroundSource({
backgroundSourceType: VirtualBackgroundSourceType.Img,
source: `${RNFS.DocumentDirectoryPath}/img.png`,
});
}
}
});
this.setState({waiting: false});
}
...
view raw app.jsx hosted with ❤ by GitHub

We can use launchImageLibrary from react-native-image-picker to get access to the image URI. We handle accessing the image based on the OS and pass in the absolute path to the image to the config.

Finally, we can update our startCall function to enable the virtual background before joining a channel:

...
startCall = async () => {
await this._engine?.enableVirtualBackground(true, this.virtualSource); // enable virtual background with the latest config
await this._engine?.joinChannel(
this.state.token,
this.state.channelName,
null,
0
);
};
...
view raw app.jsx hosted with ❤ by GitHub

Conclusion

With the Agora SDK and open-source libraries, it’s really easy to add virtual backgrounds to your video calls and live streams. There are other cool features that you can find in the API Reference. You can read about how to build a video call app in just a few lines of code here.

I invite you to join the Agora Developer Slack community. Feel free to ask any questions about the project in the #react-native-help-me channel.

RTE Telehealth 2023
Join us for RTE Telehealth - a virtual webinar where we’ll explore how AI and AR/VR technologies are shaping the future of healthcare delivery.

Learn more about Agora's video and voice solutions

Ready to chat through your real-time video and voice needs? We're here to help! Current Twilio customers get up to 2 months FREE.

Complete the form, and one of our experts will be in touch.

Try Agora for Free

Sign up and start building! You don’t pay until you scale.
Try for Free