kimv kimv - 9 months ago 47
Android Question

In Android, using exoplayer, how to fill surfaceview with a video that does not have the same aspect ratio with the device?

I have an activity that uses ExoPlayer to play a video. When I go fullscreen, unless the aspect ratio of the device is equal to that of the video, I get small black bars at the top and the bottom of the video.

This is how the layout looks :

<com.google.android.exoplayer.AspectRatioFrameLayout
android:id="@+id/video_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true">

<SurfaceView android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"/>

<View android:id="@+id/shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>

</com.google.android.exoplayer.AspectRatioFrameLayout>


I was hoping that

aspectRatioFrameLayout.setAspectRatio(mVideo.getAspectRatio());


would solve the problem, but I had no success.
Is there a way to fill the screen with the video, even if part of the video is cut off from the screen?

Answer Source

You can remove the com.google.android.exoplayer.AspectRatioFrameLayout.

Next, put a simple FrameLayout with width and height on match_parent.

Note that the SurfaceView should stay in width and height = match_parent.

Explanation: AspectRatioFrameLayout is setting the correct height of the view according to the real video ratio (calcuclating based on width, height and pixelRatio). If you don't wan't this, Exoplayer can still display the view on a SurfaceView as they was doing before. If the SurfaceView is not @Override it will not have aspect ratio.