33

I am playing video from URL on Exoplayer, it stretching the video on resizing/on using resize_mode as I have mentioned in layout file using this I am not able to maintain the aspect ratio of video.

I want to do scale type CENTER_CROP like we do in TextureSurface as mentioned in image2 but I am getting output as image1

I have tried following example

Exoplayer Demo Example

My Output (Img 1) and Expected Output (Img 2)

enter image description here

exoplayer layout code

  <com.google.android.exoplayer2.ui.SimpleExoPlayerView
      android:id="@+id/player_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:resize_mode="fill" />

With this line app:resize_mode="fill" it fit the video in screen but stretch vertically, So how can I solve this .

Sagar
  • 4,549
  • 3
  • 31
  • 44

4 Answers4

76

Following two lines helped me to play video in full-screen mode.

Using app:resize_mode in layout file this somehow help but it stretches the video as mentioned in the question picture.

<com.google.android.exoplayer2.ui.PlayerView
      android:layout_width="match_parent"
      app:resize_mode="...."
      android:layout_height="match_parent" />

Try changing AspectRatioFrameLayout to FILL,FIT,ZOOM... as per yout requirement, changing below line worked for me.

exoVideoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);

Bellow line will ensure that aspect ratio is correctly maintained even for 4:3 videos.

exoPlayer.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);

Also you can try changing VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING in exoplayer

Sagar
  • 4,549
  • 3
  • 31
  • 44
  • 1
    The line `exoVideoView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);` should be changed to `playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);`. This will ensure that aspect ratio is correctly maintained even for 4:3 videos. – ramon Sep 09 '18 at 13:54
  • Say, do you know how to do it for top-scale-crop, instead of center-crop? I asked about it here: https://stackoverflow.com/q/54216273/878126 – android developer Jan 20 '19 at 08:38
  • 2
    my player view is not in center, it placed on top (height and width is match_parent), how I can make it center ()? Note - giving top margin to playerview won't work (if I do so player view become smaller). – Bajrang Hudda Sep 26 '19 at 13:04
  • 1
    @BajrangHudda try giving padding – DragonFire May 11 '20 at 04:37
  • This is very helpful! – xrnd Sep 13 '21 at 10:53
19

To maintain center crop in exo player below code worked for me:

Java code:

playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);

or you can use from xml:

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/playerView"
    android:layout_width="match_parent"
    app:resize_mode="zoom"
    android:layout_height="match_parent" />
Touhid
  • 1,356
  • 16
  • 16
5

Following are the Resize Mode options to play around with

app:resize_mode="fixed_width"
app:resize_mode="fixed_height"
app:resize_mode="fill"
app:resize_mode="fit"
app:resize_mode="zoom"

You can try each one to see its affect on your container.

DragonFire
  • 2,836
  • 1
  • 23
  • 40
1

My issue was solved using below lines:

playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
exoPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
General Grievance
  • 4,259
  • 21
  • 28
  • 43
cyclops
  • 11
  • 3
  • In version 2.12.1. It will be this: playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL); simpleExoPlayer?.setVideoScalingMode(Renderer.VIDEO_SCALING_MODE_SCALE_TO_FIT) – VIVEK CHOUDHARY Nov 23 '20 at 09:44
  • 2
    dont forget to add `app:surface_type="texture_view"` in PlayerView – iamkdblue Feb 17 '21 at 08:44