Full screen videoview without stretching the video

how to stretch video to fit screen android
android-videoview scale to fit
android videoview fullscreen landscape
android-videoview stretch video
play video in full screen android
android videoview full screen
android videoview not filling screen
xamarin android videoview full screen

I wonder if I can get a way to let video run via videoview in full screen?

I searched a lot and tried many ways such as:

  1. Apply theme in manifest:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    

    but that does not force the video to be in full screen.

  2. Apply in activity itself:

    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    also does not force the video to be in full screen.

The only way force video to full screen is:

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

This way it results in full screen video but it stretches the video itself (elongated video) ,

I'm not applying this improper solution to my videoview, so is there is any way to do it without stretching the video?

Video Class:

public class Video extends Activity {
    private VideoView myvid;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        myvid = (VideoView) findViewById(R.id.myvideoview);
        myvid.setVideoURI(Uri.parse("android.resource://" + getPackageName() 
            +"/"+R.raw.video_1));
        myvid.setMediaController(new MediaController(this));
        myvid.requestFocus();
        myvid.start();
    }
}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

</LinearLayout>

Like this you can set the properties of the video by yourself.

Use a SurfaceView (gives you more control on the view), set it to fill_parent to match the whole screen

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
              android:orientation="vertical" 
              android:layout_width="match_parent"
              android:layout_height="fill_parent">

    <SurfaceView
        android:id="@+id/surfaceViewFrame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >
    </SurfaceView>
</Linearlayout>

then on your java code get the surface view and add your media player to it

surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
player = new MediaPlayer();
player.setDisplay(holder);

set on your media player a onPreparedListener and manually calculate the desired size of the video, to fill the screen in the desired proportion avoiding stretching the video!

player.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
                    // Adjust the size of the video
    // so it fits on the screen
    int videoWidth = player.getVideoWidth();
    int videoHeight = player.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;       
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;
    android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();

    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    surfaceViewFrame.setLayoutParams(lp);

    if (!player.isPlaying()) {
        player.start();         
    }

        }
    });

I modified this from a tutorial for video streaming that I followed some time ago, can't find it right now to reference it, if someone does please add the link to the answer!

Hope it helps!

EDIT

Ok, so, if you want the video to occupy the whole screen and you don't want it to stretch you will end up with black stripes in the sides. In the code I posted we are finding out what is bigger, the video or the phone screen and fitting it the best way we can.

There you have my complete activity, streaming a video from a link. It's 100% functional. I can't tell you how to play a video from your own device because I don't know that. I'm sure you will find it in the documentation here or here.

public class VideoPlayer extends Activity implements Callback, OnPreparedListener, OnCompletionListener, 
    OnClickListener {   

private SurfaceView surfaceViewFrame;
private static final String TAG = "VideoPlayer";
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private ImageView pause;
private MediaPlayer player; 
private Timer updateTimer;
String video_uri = "http://daily3gp.com/vids/familyguy_has_own_orbit.3gp";  


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videosample);       


    pause = (ImageView) findViewById(R.id.imageViewPauseIndicator);
    pause.setVisibility(View.GONE);
    if (player != null) {
        if (!player.isPlaying()) {
            pause.setVisibility(View.VISIBLE);
        }
    }


    surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
    surfaceViewFrame.setOnClickListener(this);
    surfaceViewFrame.setClickable(false);

    progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);

    holder = surfaceViewFrame.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    player = new MediaPlayer();
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setScreenOnWhilePlaying(true);
    player.setDisplay(holder);
}

private void playVideo() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    player.setDataSource(video_uri);
                    player.prepare();
                } catch (Exception e) { // I can split the exceptions to get which error i need.
                    showToast("Error while playing video");
                    Log.i(TAG, "Error");
                    e.printStackTrace();
                } 
            }
        }).start();     
}

private void showToast(final String string) {
    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(VideoPlayer.this, string, Toast.LENGTH_LONG).show();
            finish();
        }
    });
}


public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    playVideo();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}
//prepare the video
public void onPrepared(MediaPlayer mp) {        
    progressBarWait.setVisibility(View.GONE);

    // Adjust the size of the video
    // so it fits on the screen
    int videoWidth = player.getVideoWidth();
    int videoHeight = player.getVideoHeight();
    float videoProportion = (float) videoWidth / (float) videoHeight;       
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;
    android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();

    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    surfaceViewFrame.setLayoutParams(lp);

    if (!player.isPlaying()) {
        player.start();         
    }
    surfaceViewFrame.setClickable(true);
}

// callback when the video is over
public void onCompletion(MediaPlayer mp) {
    mp.stop();
    if (updateTimer != null) {
        updateTimer.cancel();
    }
    finish();
}

//pause and resume
public void onClick(View v) {
    if (v.getId() == R.id.surfaceViewFrame) {
         if (player != null) {
            if (player.isPlaying()) {
                player.pause();
                pause.setVisibility(View.VISIBLE);
            } else {
                player.start();
                pause.setVisibility(View.GONE);
            }
        }
    }
}

}

Full screen VideoView with stretched video. [Android], How to make a video full screen using an Exoplayer without stretching it. I'm using an ExoPlayer to show videos, currently I just maintain the aspect ratio as� Home » Android » java – Full screen videoview without stretching the video java – Full screen videoview without stretching the video Posted by: admin May 10, 2020 Leave a comment

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView1 = (VideoView) findViewById(R.id.videoview);
                String SrcPath = "/mnt/sdcard/final.mp4";
        videoView1.setVideoPath(SrcPath);
        videoView1.setMediaController(new MediaController(this));
        videoView1.requestFocus();      
        videoView1.start();     
    }
}




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

try this it's working for me

Enable Stretch To Fit Override option for video in fullscreen , Currently, in Brave on Android, all 16:9 videos in fullscreen-landscape mode play by .com/2013/04/23/android-how-to-stretch-video-to-fill-videoview-area/ https://www.droidviews.com/enable-immersive-mode-without-root/. set on your media player a onPreparedListener and manually calculate the desired size of the video, to fill the screen in the desired proportion avoiding stretching the video!

The current upvoted solution works, but there may be a simpler solution to the original problem. A commenter correctly pointed out that you could resize a VideoView using the same methodology without the cost of converting everything to a SurfaceView. I tested this in one of my apps and it seems to work. Just add the calculated layout parameters to the VideoView in the OnPreparedListener callback:

mInspirationalVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // mMediaPlayer = mp;

            mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    if(isPlaying = true) {
                        stopPosition = 0;
                        mp.start();
                        mVideoProgressTask = new VideoProgress();
                        mVideoProgressTask.execute();
                    }
                }
            });


            // so it fits on the screen
            int videoWidth = mp.getVideoWidth();
            int videoHeight = mp.getVideoHeight();
            float videoProportion = (float) videoWidth / (float) videoHeight;


            DisplayMetrics mDisplayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);

            float screenWidth = mDisplayMetrics.widthPixels;
            float screenHeight = mDisplayMetrics.heightPixels;

            float screenProportion = (float) screenWidth / (float) screenHeight;
            android.view.ViewGroup.LayoutParams lp = mInspirationalVideoView.getLayoutParams();

            if (videoProportion > screenProportion) {
                lp.width = screenWidth;
                lp.height = (int) ((float) screenWidth / videoProportion);
            } else {
                lp.width = (int) (videoProportion * (float) screenHeight);
                lp.height = screenHeight;
            }

            mInspirationalVideoView.setLayoutParams(lp);

           ...

        }
    });

android.media.MediaPlayer.getVideoHeight java code examples , Full screen videoview without stretching the video. int videoHeight = player. getVideoHeight(); float videoProportion = (float) videoWidth / (float) videoHeight; � First thing we need is a button in UI that can be used to view video in full screen and exit full screen to watch in normal mode. The best component to place view-full-screen and exit-full-screen button is in MediaController as MediaController is associated with VideoView and is visible on touching the video.

Here is my function which works for the full screen video without stretching it. It will automatically crop the sides of the video. It worked both portrait and landscape modes.

It was actually taken from the answer.

    public void onPrepared(MediaPlayer mp) {
        int videoWidth = mediaPlayer.getVideoWidth();
        int videoHeight = mediaPlayer.getVideoHeight();

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        float scaleY = 1.0f;
        float scaleX = (videoWidth * screenHeight / videoHeight) / screenWidth;

        int pivotPointX = (int) (screenWidth / 2);
        int pivotPointY = (int) (screenHeight / 2);

        surfaceView.setScaleX(scaleX);
        surfaceView.setScaleY(scaleY);
        surfaceView.setPivotX(pivotPointX);
        surfaceView.setPivotY(pivotPointY);

        mediaPlayer.setLooping(true);
        mediaPlayer.start();
    }

Full screen videoview without stretching the video, I wonder if I can get a way to let video run via videoview in full screen? I searched a lot and tried many ways such as: Apply theme in manifest:� The VideoView class included in the Android SDK provides a very convenient wrapper of the Surface View class. It takes care of computing its measurement from the source video so that it can be used in any layout manager. By default Android will scale the video to fit the VideoView but keeping the video aspect ratio.

A SurfaceView gives u an optimized drawing surface

public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
        private MediaPlayer media = null;
        //...

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            media = new MediaPlayer();
            mSurfaceView = (SurfaceView) findViewById(R.id.surface);
            //...
        }
    }

MediaPlayer calls should be wrapped in a try{}.

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        media.setDataSource("android.resource://" + getPackageName() 
            +"/"+R.raw.video_);
        media.prepare();

            int videoWidth = mp.getVideoWidth();
        int videoHeight = mp.getVideoHeight();

            int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

            android.view.

ViewGroup.LayoutParams layout = mSurfaceView.getLayoutParams();

    layout.width = screenWidth;

    layout.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    mSurfaceView.setLayoutParams(layout);        

    mp.start();
}

FullscreenVideoView — VideoView gone fullscreen, It's a combination of several classes, that handle the orientation change, a video controller, a media player, and the actual FullscreenVideoView� No need to create a custom view extending from VideoView. Just set a value big enough to android:layout_width. This will set the widthSpecMode of the video view to View.MeasureSpec.AT_MOST and then the onMeasure () method of VideoView will auto-adjust its width keeping the ratio.

Android : How to stretch video to use whole area of VideoView (sode , Android : How to stretch video to use whole area of VideoView (sode snippets). StretchVideoView.java. package com.raulete.dev.stretchvideoview.utils;. I am trying to stretch my video view inside the linear layout to full screen but it doesn't match the device screen. Full screen videoview without stretching the

Android - How to stretch video to fill VideoView area, By default Android will scale the video to fit the VideoView but keeping the video aspect ratio. That means that depending on the device's screen� To watch videos on their original aspect ratio (without stretching) but covering the entire screen follow the steps above but select the correct aspect ratio under Video -> Crop. Default must be kept under Aspect Ratio for this to work or nothing will need to be cropped.

Android – How to stretch video to fill complete screen – nthreads, You may want to display video in parent container area in full width and height but VideoView doesn't provide an explicit way to specify the� No need of code to play video in full screen mode. Apply the following layout format over the xml containing the videoview it will for sure will play the video in full screen mode. as it is running mine :) Hope it helps

Comments
  • By simply removing 4 android:layout_alignXXX in layout xml file should make your VideoView use as much screen space as possible while remain video's aspect ratio. This is how the API is design and suppose to work.
  • @yorkw i know that , normally it take 1/3 of screen with normal video display , also im not appling that xml with 4 android:layout_alignXXX to my app ,its an improper solution i found to force to full screen ,i want any proper way to force it to be in full screen but(without stretching ) ,
  • I would assume this is the expected result. If you play in portrait mode, the video usually take full width but leave much space on top and bottom, if play in landscape mode, the video usually take full height but leave some space on side.
  • @yorkw android default player and many custom player play any video in full screen without spacing so that there must be a trick or code hack to do that and this what im looking for my dear
  • Do they still remain aspect ratio? I doubt that. there are many device which has different screen size. take Samsung Galaxy S2 as an example, the screen size is 480 x 800, which is 5:3 in landscape mode, which you can see is not a normal aspect ratio like 4:3 or 16:9.
  • FIRST OF ALL thanks for your answer , would you please write full code , because this is the first time i used videoview and never use surfaceview or MediaPlayer, im not expert in android , please also i cant understand this (manually calculate the desired size of the video, to fill the screen in the desired proportion ) , also i want finally when playing my videos in any mobile phone to occupy full screen without distortion or stretching , this question will be apart of big project so my app will have many videos with different length and size , thanks alot
  • ALSO I ADD THIS LINE :final MediaPlayer player = MediaPlayer.create(this,R.raw.video_1); then it play only audio of video not video it self , thanks
  • If the answer is what you seek for please don't forget to accept it! I'm glad I could help!
  • This code will resize the video but will not stretch it. if you want the video to be fullscreen I imagine you need several resolutions and you need to pick one that matches with your phone, but keep in mind I'm no expert in media playing. You can trace the exception and see what is wrong. My first guess is some broken link or mistype in the URI!
  • Just as an aside, I don't think there's any reason to go through all the SurfaceView crazyness. You can just use a VideoView like you normally would, but apply that sizing code to its LayoutParameters in the onPrepared callback.