0

I am using Exoplayer to load videos from the Internet.

When the videos is loaded and starts to display inside the container, the video automatically stretches while the flag MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT is set on the video renderer.

this._videoRenderer = new MediaCodecVideoTrackRenderer(this._context, source, MediaCodecSelector.DEFAULT, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);

Layout:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/selfie_slider_slide_thumbnail"
        android:layout_gravity="center|center_vertical"
        android:visibility="gone" />

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/selfie_slider_slide_video"
        android:layout_gravity="center|center_vertical" />
</LinearLayout>

The image is correctly displayed, but not the video. Here are two examples to correctly illustrate the problem:

  • This is the thumbnail inside the container This is the thumbnail inside the container

  • This is the video once it is loaded. You can see that it is stretched (don't pay attention to the black lines, this is a bad cropping) This is the video once it is loaded

I guess I could be resizing the container according to the thumbnail size, but that would mean that I would need to resize the container every time the video changes, so I'm trying to find a better way to do that. If anyone has a guess, I'm taking it!

GeoffreyB
  • 1,791
  • 4
  • 20
  • 36

1 Answers1

2

You can either use the Exoplayer AspectRatioFrameLayout or in previous version they used a AspectRatioTextureView. You'll need to receive the onVideoSizeChanged event and update the TextureView/FrameLayout ratio :

/**
 * @param width                    video width
 * @param height                   video width
 * @param unappliedRotationDegrees ?
 * @param pixelRatio               (optional) pixel ratio
 */
@Override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelRatio) {
    if (!mRatioAlreadyCalculated && mVideoWidthHeightRatio != (float) width / height) {
        mVideoWidthHeightRatio = ((float) width / height) * pixelRatio;
        mRatioAlreadyCalculated = true;
    }
    updateVideoRatio();
}
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
  • Do you perhaps know how to scale-crop it to work like center-crop, but at the top? I've asked about it here: https://stackoverflow.com/q/54216273/878126 – android developer Jan 16 '19 at 11:41