Android VideoView resize according every device

170 Views Asked by At

I am playing video in Android Video view.Issue is video is not resizing as per device ratio. On big devices it stretched or in small device it is skeqzes. Is there any way to maintain ratio and fill full width like center crop.

I have checked following answers:

Resize video to fit the VideoView

Android VideoView orientation change with buffered video

But nothing works.

My code :

mBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {

                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                            mBinding.placeholder.setVisibility(View.GONE);

                            return true;
                        }
                        return false;
                    }
                });

                mp.setLooping(true);
            }
        });

XML :

  <VideoView
            android:id="@+id/videoView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

  </VideoView>
1

There are 1 best solutions below

0
On

Wrap the whole xml layout into a RelativeLayout. then modify your VideoView tag as following-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoView"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>