Skip to content

Commit d447731

Browse files
authored
Update video-effects-android.md
Add Background Replacement to Android
1 parent 89f2cde commit d447731

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

articles/communication-services/quickstarts/voice-video-calling/includes/video-effects/video-effects-android.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,70 @@ public void disableBackgroundBlur() {
120120
videoEffectsFeature.disableEffect(backgroundBlurEffect);
121121
}
122122
```
123+
124+
### Background replacement
125+
126+
Background Replacement is a Video Effect that allows a person's background to be replacementred. In order to use Background Video Effect, you need to obtain a `VideoEffectsLocalVideoStreamFeature` feature from a valid `LocalVideoStream`.
127+
128+
To enable Background Replacement Video Effect:
129+
130+
- Create a method that obtains the `VideoEFfects` Feature subscribes to the events:
131+
132+
```java
133+
private void handleOnVideoEffectEnabled(VideoEffectEnabledEvent args) {
134+
Log.i("VideoEfects", "Effect enabled for effect " + args.getVideoEffectName());
135+
}
136+
private void handleOnVideoEffectDisabled(VideoEffectDisabledEvent args) {
137+
Log.i("VideoEfects", "Effect disabled for effect " + args.getVideoEffectName());
138+
}
139+
private void handleOnVideoEffectError(VideoEffectErrorEvent args) {
140+
Log.i("VideoEfects", "Error " + args.getCode() + ":" + args.getMessage()
141+
+ " for effect " + args.getVideoEffectName());
142+
}
143+
144+
VideoEffectsLocalVideoStreamFeature videoEffectsFeature;
145+
public void createVideoEffectsFeature() {
146+
videoEffectsFeature = currentVideoStream.feature(Features.LOCAL_VIDEO_EFFECTS);
147+
videoEffectsFeature.addOnVideoEffectEnabledListener(this::handleOnVideoEffectEnabled);
148+
videoEffectsFeature.addOnVideoEffectDisabledListener(this::handleOnVideoEffectDisabled);
149+
videoEffectsFeature.addOnVideoEffectErrorListener(this::handleOnVideoEffectError);
150+
}
151+
152+
```
153+
154+
- Create a new Background Replacement Video Effect object:
155+
156+
```java
157+
BackgroundReplacementEffect backgroundReplacementVideoEffect = new BackgroundReplacementEffect();
158+
```
159+
160+
- Set a custom background by passing in the image through a buffer.
161+
162+
```java
163+
//example of where we can get an image from, in this case, this is from assets in Android folder
164+
InputStream inputStream = getAssets().open("image.jpg");
165+
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
166+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
167+
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
168+
byte[] data = stream.toByteArray();
169+
ByteBuffer dataBuffer = ByteBuffer.allocateDirect(data.length);
170+
dataBuffer.put(data);
171+
dataBuffer.rewind();
172+
backgroundReplacementVideoEffect.setBuffer(dataBuffer);
173+
```
174+
175+
- Call `EnableEffect` on the `videoEffectsFeature` object:
176+
177+
```java
178+
public void enableBackgroundReplacement() {
179+
videoEffectsFeature.enableEffect(backgroundReplacementEffect);
180+
}
181+
```
182+
183+
To disable Background Replacement Video Effect:
184+
185+
```java
186+
public void disableBackgroundReplacement() {
187+
videoEffectsFeature.disableEffect(backgroundReplacementEffect);
188+
}
189+
```

0 commit comments

Comments
 (0)