Virtual Background - Android
Virtual backgrounds allow you to replace your actual background during video calls or meetings with an image, a solid color, or even a blur effect. This can be useful for:
-
Privacy: Hide your messy room or anything you don't want others to see.
-
Focus: Minimize distractions and keep the focus on the conversation.
-
Fun: Liven up the call with a cool background theme.
Virtual Background using MediaEffects
library
The MediaEffects
library enables the integration of sophisticated virtual background effects within your video applications. By leveraging this library, users can replace their physical backgrounds with custom images or static backdrops, creating a more immersive and engaging experience.
The Virtual Background feature in VideoSDK can be utilized regardless of the meeting environment, including the pre-call screen.
Install the library
You can install our Android library by using Maven Central package repositories.
- Add the dependency in your
app/build.gradle
file.
dependencies {
implementation 'live.videosdk:videosdk-media-effects:0.0.2'
// other app dependencies
}
Apply Virtual Background
-
The
applyVideoProcessor()
method of theVideoSDK
class enables dynamic changes to the video background during a live session. -
This method accepts an instance of the
VideoFrameProcessor
class as its parameter. -
The following classes implement the
VideoFrameProcessor
interface. Depending on your requirements, you can create an instance of the appropriate class and pass it to theapplyVideoProcessor()
method:BackgroundImageProcessor
: TheBackgroundImageProcessor
class takes aUri
as a parameter and uses it as the video stream background.BackgroundBlurProcessor
: TheBackgroundBlurProcessor
class takes afloat
value for the blur radius and aContext
to apply a blur effect to the video stream background. It supports range between 0 to 25.BackgroundColorProcessor
: TheBackgroundColorProcessor
class takes anint
value for the color and sets it as the background for the video stream.
Change Virtual Background
-
To change the virtual background, users can utilize a set of setters that allow easy switching between a new image, a blur effect, or a solid color. This functionality supports dynamic customization, enhancing the user's video experience by enabling seamless background updates in real-time.
-
You can use following setters to change Virtual Background:
setBackgroundSource(Uri uri)
: This method of theBackgroundImageProcessor
class takes aUri
as a parameter to update the current background image for the video stream.setBlurRadius(float opacity)
: This method of theBackgroundBlurProcessor
class takes afloat
value for the blur radius to adjust the blur effect of the current background.setBackgroundColor(int color)
: This method of theBackgroundColorProcessor
class takes anint
value as a parameter to update the current background color for the video stream.
Remove Virtual Background
- The
removeVideoProcessor()
method from theVideoSDK
class provides users with a convenient way to revert their video background to its original state, removing any previously applied virtual background. This functionality ensures flexibility for users to switch between virtual and real backgrounds as needed.
Example
- Kotlin
- Java
Class MeetingActivity : AppCompatActivity() {
private var backgroundImageProcessor: BackgroundImageProcessor? = null
private var backgroundBlurProcessor: BackgroundBlurProcessor? = null
private var backgroundColorProcessor: BackgroundColorProcessor? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
//To Apply Virtual Background
findViewById<View>(R.id.applyBackground).setOnClickListener { view: View? ->
val uri =
Uri.parse("https://st.depositphotos.com/2605379/52364/i/450/depositphotos_523648932-stock-photo-concrete-rooftop-night-city-view.jpg")
backgroundImageProcessor = BackgroundImageProcessor(uri)
// backgroundBlurProcessor = BackgroundBlurProcessor(25f, this)
// backgroundColorProcessor = BackgroundColorProcessor(Color.BLUE)
VideoSDK.applyVideoProcessor(backgroundColorProcessor)
}
//To Change Virtual Background
findViewById<View>(R.id.changeBackground).setOnClickListener { view: View? ->
val newUri =
Uri.parse("https://images.pexels.com/photos/531880/pexels-photo-531880.jpeg?cs=srgb&dl=pexels-pixabay-531880.jpg&fm=jpg")
backgroundImageProcessor!!.setBackgroundSource(newUri)
// backgroundColorProcessor!!.setBackgroundColor(Color.CYAN)
// backgroundBlurProcessor!!.setBlurRadius(17f)
}
//To Remove Virtual Background
findViewById<View>(R.id.changeBackground).setOnClickListener { view: View? ->
VideoSDK.removeVideoProcessor()
}
}
}
public class MeetingActivity extends AppCompatActivity {
private BackgroundImageProcessor backgroundImageProcessor;
private BackgroundBlurProcessor backgroundBlurProcessor;
private BackgroundColorProcessor backgroundColorProcessor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
//To Apply Virtual Background
findViewById(R.id.applyBackground).setOnClickListener(view -> {
Uri uri = Uri.parse("https://st.depositphotos.com/2605379/52364/i/450/depositphotos_523648932-stock-photo-concrete-rooftop-night-city-view.jpg");
backgroundImageProcessor = new BackgroundImageProcessor(uri);
// backgroundBlurProcessor = new BackgroundBlurProcessor(25,this);
// backgroundColorProcessor = new BackgroundColorProcessor(Color.BLUE);
VideoSDK.applyVideoProcessor(backgroundColorProcessor);
});
//To Change Virtual Background
findViewById(R.id.changeBackground).setOnClickListener(view -> {
Uri newUri = Uri.parse("https://images.pexels.com/photos/531880/pexels-photo-531880.jpeg?cs=srgb&dl=pexels-pixabay-531880.jpg&fm=jpg");
backgroundImageProcessor.setBackgroundSource(newUri);
// backgroundColorProcessor.setBackgroundColor(Color.CYAN);
// backgroundBlurProcessor.setBlurRadius(17);
});
//To Remove Virtual Background
findViewById(R.id.removeBackground).setOnClickListener(view -> {
VideoSDK.removeVideoProcessor();
});
}
}
API Reference
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord