Full screen videoview without stretching the video Full screen videoview without stretching the video android android

Full screen videoview without stretching the video


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";  @Overrideprotected 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 videopublic 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 overpublic void onCompletion(MediaPlayer mp) {    mp.stop();    if (updateTimer != null) {        updateTimer.cancel();    }    finish();}//pause and resumepublic 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);            }        }    }}}


    @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


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);           ...        }    });