1

I want to set a video that fills the screen and preserves its aspect ratio. However, I do not want the video to match the aspect ratio of the phone.

If my screen's aspect ratio is 18.9:9 and the video's aspect ratio is 16:9, then the video appears stretched when I put it in a VideoView that inherits fill_parent. If I don't set fill_parent then the video preserves its aspect ratio but only occupies less than a 1/3 of the screen because the aspect ratio is determined by its width, not its height. This is not desirable.

I want to fill the screen vertically with the video, regardless if part of the width is clipped off.

Current Code:

window.setFormat(PixelFormat.TRANSLUCENT)
val videoView = findViewById < VideoView > (videoView)
val video = Uri.parse("android.resource://" + packageName + "/" +
    R.raw.bgvideo)
videoView.setVideoURI(video)
videoView.setOnPreparedListener {
    mp: MediaPlayer - >
        mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
    mp.isLooping = true
    mp.setScreenOnWhilePlaying(false)
}
videoView.start()

XML:

<VideoView
    android:id="@+id/videoView"
    android:layout_width="2000dp"
    android:layout_height="match_parent" />

<!--- Sample Foreground Content !--->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="24dp"
    android:contentDescription="@string/logo"
    android:src="@drawable/logo" />

Current Result:

enter image description here

Desired Outcome: enter image description here

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • There was no resolution to that problem and it was posted 6 years ago. We should probably keep my question up. – Martin Erlic Jun 18 '19 at 02:59
  • If you have the resolution of your video, why not just adjust the dimension of the `VideoView` programatically. – Israel dela Cruz Jun 18 '19 at 03:13
  • @IsraeldelaCruz The aspect ratio of the video is 16:9 but the aspect ratio of the screen is 18.9:9. If I use fill_parent the video appears stretched. I want the video to fill the screen, even though part of the height will be cropped off. – Martin Erlic Jun 18 '19 at 03:25

1 Answers1

1

Adjust your VideoView's dimension programatically.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--parent needs to be matching the screen height-->
    <!--VideoView layout_width doesn't matter-->

    <VideoView
        android:id="@+id/video_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>
// at onCreate or anywhere

        // post, because view dimension is not initialized yet in here
        videoView.post(new Runnable() {
            @Override
            public void run() {
                // do resizing here
                // AR = aspect ratio

                float videoARWidth = 16.f;
                float videoARHeight = 9.f;

                // Phone screen aspect ratio height
                float screenARHeight = 9.f;

                // scale to screen AR height
                float videoScale = screenARHeight / videoARHeight;

                float videoARRatio = videoARWidth / videoARHeight;

                // scale the ratio to screen
                float videoScaledARRatio = videoARRatio * videoScale;

                ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();

                // make sure the VideoView matches the screen height
                layoutParams.width = (int)(videoView.getHeight() * videoScaledARRatio);
                videoView.setLayoutParams(layoutParams);
            }
        });

This code does not care about the screen width.

The width will always scale to the screen height. Actually, it will always scale to its(VideoView) own height. But as I commented on the code, use layout_height = "match_parent" to match the screen.

I'm not sure if you want the width to fit if the screen width is bigger.

The video width will not fill the screen if the screen width is bigger.

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
Israel dela Cruz
  • 794
  • 1
  • 5
  • 11
  • Thanks for the detailed answer. Just going through it now. What is AR height? Also can you provide your XML? Your code doesn't seem to be making a difference unless I am missing something. My phone's aspect ratio is 19.5:9. I want the height to fill the screen, but for the width to not to be bounded by the screen (it can bleed over). – Martin Erlic Jun 18 '19 at 09:28
  • 1
    I've provided the xml. AR is aspect ratio. – Israel dela Cruz Jun 18 '19 at 15:56
  • I spoke too soon. This does in fact achieve the effect I want. Just have to make sure the parent layout is a FrameLayout. You might also want to post your solution here: https://stackoverflow.com/questions/13603553/videoview-to-match-parent-height-and-keep-aspect-ratio, as I believe no answer has been selected yet and none of them have worked for me. – Martin Erlic Jun 18 '19 at 19:55