Skip to main content
Version: 4.x.x

Virtual Background - iOS

Virtual backgrounds let you replace your physical background with a digital image. You can use a preloaded background image or supply your own via a CDN.

Virtual Background using VideoSDKBackgroundProcessor​

The VideoSDKBackgroundProcessor class replaces your background for you. Here's how to use it:

Toggle Virtual Background​

  • To enable the virtual background, pass a VideoSDKBackgroundProcessor instance to the setVideoProcessor method of the Room class.

  • To disable the virtual background, call removeVideoProcessor() (or pass nil to setVideoProcessor). The normal camera stream comes back.

caution

VideoSDKBackgroundProcessor downloads the background image when you create it or call changeBackground. While the processor is on and has no image yet, no video is sent from your camera. If the download fails, no video is sent until a new image loads. Create the processor before you turn it on, and use an image URL that loads quickly.

Example Code:

class RoomViewController: UIViewController {
var room: Room?
//variable for keeping track of Virtual Background
var isVirtualBackground: Bool = false

//create an instance of VideoSDKBackgroundProcessor with the source url for the background image
//(make sure background is in portrait, else might get undesired output).
let processor = VideoSDKBackgroundProcessor(
backgroundSource: URL(string: "https://cdn.videosdk.live/virtual-background/cloud.jpeg")!
)

// Button when tapped will toggle the virtual background for local participant
@IBAction func toggleVirtualBackground(_ sender: Any) {

if isVirtualBackground {
// Turn off virtual background
room?.removeVideoProcessor()
isVirtualBackground = false
} else {
// Turn on virtual background
room?.setVideoProcessor(processor: processor)
isVirtualBackground = true
}
}
}

Changing Background Image​

You can also change the background image dynamically using the changeBackground method of the VideoSDKBackgroundProcessor class. The current background stays until the new image has downloaded.

extension RoomViewController {
// uses the processor created in the previous example

// Button when tapped will change the background image of the virtual background
@IBAction func changeBackgroundTapped(_ sender: Any) {
// URL of new background image
let backgroundSource = URL(string: "https://cdn.videosdk.live/virtual-background/san-fran.jpeg")!

// Call changeBackground method of VideoSDKBackgroundProcessor
self.processor.changeBackground(backgroundSource: backgroundSource)
}
}

API Reference​

The API references for all the methods utilized in this guide are provided below.

Got a Question? Ask us on discord