3

I use ExoPlayer library for loading videos from my server and I want to scale and crop the videos to keep their aspect ratio. The videos playing fine and everything are fine even when I change the volume of the video. I tried to use the setVideoScalingMode function on the SimpleExoPlayer and passed:

C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING

Nothing happens and I also tried to change the resize attribute to fill/fit. Still no scaling and cropping.

Zach Bublil
  • 857
  • 10
  • 28

3 Answers3

10

After a lot of researchers all over the internet and after a lot of time debugging the exoplayer library code in order to understand why the scaling doesn't work. I found another option for the "resizeMode" attribute via code instead of the xml option which is "RESIZE_MODE_ZOOM" and it cropped the video to maintain the ratio. Hope it will someone who will encounter the problem.

Zach Bublil
  • 857
  • 10
  • 28
  • It kinda solves it, but I still see some deformation on the video's width. It's a bit stretched up, but as soon as the video plays or I press "Back", the video width is restored to centerCrop. Is anyone aware of the reason to this deformation? – Juan José Melero Gómez Nov 27 '19 at 12:06
  • I'm not sure, but I think the formation occurs only after the video is fully loaded and the surface of the video changes its size. – Zach Bublil Nov 27 '19 at 12:24
  • Sorry, my problem. I was calling `mExoplayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)` in addition to changing the resize mode. You musn't do that. – Juan José Melero Gómez Nov 27 '19 at 13:56
2

Java or Kotlin

playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);

XML

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/playerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:resize_mode="zoom"/>
Arun NS
  • 1,900
  • 1
  • 8
  • 8
0

In compose you can set like this:

    AndroidView(
        factory = { mContext ->
            StyledPlayerView(mContext).apply {
                resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
                player = exo
            }
        },
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black)
    )
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36