Virtual Background - Android
Virtual backgrounds enhance your live stream experience by allowing you to replace your physical background with a digital image or apply a blur effect. This is ideal for creators and streamers who want to maintain privacy, reduce distractions, or add a personal touch to their broadcast. You can use a preloaded background image or supply your own via a CDN.
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.
Install the library
You can install our Android library by using Maven Central package repositories.
- Add the dependency in your app/build.gradlefile.
dependencies {
	implementation 'live.videosdk:videosdk-media-effects:0.0.2'
	// other app dependencies
}
Apply Virtual Background
- 
The applyVideoProcessor()method of theVideoSDKclass enables dynamic changes to the video background during a live session.
- 
This method accepts an instance of the VideoFrameProcessorclass as its parameter.
- 
The following classes implement the VideoFrameProcessorinterface. Depending on your requirements, you can create an instance of the appropriate class and pass it to theapplyVideoProcessor()method:- BackgroundImageProcessor: The- BackgroundImageProcessorclass takes a- Urias a parameter and uses it as the video stream background.
- BackgroundBlurProcessor: The- BackgroundBlurProcessorclass takes a- floatvalue for the blur radius and a- Contextto apply a blur effect to the video stream background. It supports range between 0 to 25.
- BackgroundColorProcessor: The- BackgroundColorProcessorclass takes an- intvalue 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 the- BackgroundImageProcessorclass takes a- Urias a parameter to update the current background image for the video stream.
- setBlurRadius(float opacity): This method of the- BackgroundBlurProcessorclass takes a- floatvalue for the blur radius to adjust the blur effect of the current background.
- setBackgroundColor(int color): This method of the- BackgroundColorProcessorclass takes an- intvalue as a parameter to update the current background color for the video stream.
 
Remove Virtual Background
- The removeVideoProcessor()method from theVideoSDKclass 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 LiveStreamActivity : 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 LiveStreamActivity 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

