Demo app fixes

- Do not fail silently if selectedEffects is null.
- Do not add an empty overlay effect to the Transformer to avoid
  transcoding video all the time.

PiperOrigin-RevId: 499168759
This commit is contained in:
kimvde 2023-01-03 11:05:45 +00:00 committed by Marc Baechinger
parent 2cda644915
commit fed93723a3

View File

@ -18,6 +18,7 @@ package androidx.media3.demo.transformer;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE; import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState; import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import static androidx.media3.transformer.Transformer.PROGRESS_STATE_NOT_STARTED; import static androidx.media3.transformer.Transformer.PROGRESS_STATE_NOT_STARTED;
import android.app.Activity; import android.app.Activity;
@ -383,12 +384,8 @@ public final class TransformerActivity extends AppCompatActivity {
private ImmutableList<Effect> createVideoEffectsFromBundle(Bundle bundle) private ImmutableList<Effect> createVideoEffectsFromBundle(Bundle bundle)
throws PackageManager.NameNotFoundException { throws PackageManager.NameNotFoundException {
@Nullable
boolean[] selectedEffects = boolean[] selectedEffects =
bundle.getBooleanArray(ConfigurationActivity.VIDEO_EFFECTS_SELECTIONS); checkStateNotNull(bundle.getBooleanArray(ConfigurationActivity.VIDEO_EFFECTS_SELECTIONS));
if (selectedEffects == null) {
return ImmutableList.of();
}
ImmutableList.Builder<Effect> effects = new ImmutableList.Builder<>(); ImmutableList.Builder<Effect> effects = new ImmutableList.Builder<>();
if (selectedEffects[ConfigurationActivity.DIZZY_CROP_INDEX]) { if (selectedEffects[ConfigurationActivity.DIZZY_CROP_INDEX]) {
effects.add(MatrixTransformationFactory.createDizzyCropEffect()); effects.add(MatrixTransformationFactory.createDizzyCropEffect());
@ -507,13 +504,18 @@ public final class TransformerActivity extends AppCompatActivity {
effects.add(MatrixTransformationFactory.createZoomInTransition()); effects.add(MatrixTransformationFactory.createZoomInTransition());
} }
effects.add(createOverlayEffectFromBundle(bundle, selectedEffects)); @Nullable OverlayEffect overlayEffect = createOverlayEffectFromBundle(bundle, selectedEffects);
if (overlayEffect != null) {
effects.add(overlayEffect);
}
return effects.build(); return effects.build();
} }
@Nullable
private OverlayEffect createOverlayEffectFromBundle(Bundle bundle, boolean[] selectedEffects) private OverlayEffect createOverlayEffectFromBundle(Bundle bundle, boolean[] selectedEffects)
throws PackageManager.NameNotFoundException { throws PackageManager.NameNotFoundException {
ImmutableList.Builder<TextureOverlay> overlays = new ImmutableList.Builder<>(); ImmutableList.Builder<TextureOverlay> overlaysBuilder = new ImmutableList.Builder<>();
if (selectedEffects[ConfigurationActivity.OVERLAY_LOGO_AND_TIMER_INDEX]) { if (selectedEffects[ConfigurationActivity.OVERLAY_LOGO_AND_TIMER_INDEX]) {
float[] logoPositioningMatrix = GlUtil.create4x4IdentityMatrix(); float[] logoPositioningMatrix = GlUtil.create4x4IdentityMatrix();
Matrix.translateM( Matrix.translateM(
@ -528,7 +530,7 @@ public final class TransformerActivity extends AppCompatActivity {
/* left= */ 0, /* top= */ 0, logo.getIntrinsicWidth(), logo.getIntrinsicHeight()); /* left= */ 0, /* top= */ 0, logo.getIntrinsicWidth(), logo.getIntrinsicHeight());
TextureOverlay logoOverlay = DrawableOverlay.createStaticDrawableOverlay(logo, logoSettings); TextureOverlay logoOverlay = DrawableOverlay.createStaticDrawableOverlay(logo, logoSettings);
TextureOverlay timerOverlay = new TimerOverlay(); TextureOverlay timerOverlay = new TimerOverlay();
overlays.add(logoOverlay, timerOverlay); overlaysBuilder.add(logoOverlay, timerOverlay);
} }
if (selectedEffects[ConfigurationActivity.BITMAP_OVERLAY_INDEX]) { if (selectedEffects[ConfigurationActivity.BITMAP_OVERLAY_INDEX]) {
OverlaySettings overlaySettings = OverlaySettings overlaySettings =
@ -541,7 +543,7 @@ public final class TransformerActivity extends AppCompatActivity {
BitmapOverlay.createStaticBitmapOverlay( BitmapOverlay.createStaticBitmapOverlay(
Uri.parse(checkNotNull(bundle.getString(ConfigurationActivity.BITMAP_OVERLAY_URI))), Uri.parse(checkNotNull(bundle.getString(ConfigurationActivity.BITMAP_OVERLAY_URI))),
overlaySettings); overlaySettings);
overlays.add(bitmapOverlay); overlaysBuilder.add(bitmapOverlay);
} }
if (selectedEffects[ConfigurationActivity.TEXT_OVERLAY_INDEX]) { if (selectedEffects[ConfigurationActivity.TEXT_OVERLAY_INDEX]) {
OverlaySettings overlaySettings = OverlaySettings overlaySettings =
@ -558,9 +560,11 @@ public final class TransformerActivity extends AppCompatActivity {
overlayText.length(), overlayText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextOverlay textOverlay = TextOverlay.createStaticTextOverlay(overlayText, overlaySettings); TextOverlay textOverlay = TextOverlay.createStaticTextOverlay(overlayText, overlaySettings);
overlays.add(textOverlay); overlaysBuilder.add(textOverlay);
} }
return new OverlayEffect(overlays.build());
ImmutableList<TextureOverlay> overlays = overlaysBuilder.build();
return overlays.isEmpty() ? null : new OverlayEffect(overlays);
} }
@RequiresNonNull({ @RequiresNonNull({