20

I am playing local video file using MediaPlayer and SurfaceView. SurfaceView is the only control in activity, while my video files are QVGA or other. Problem is that video is getting stretched, How can i play video in its original size e.g. qvga with remaining area black.

From iteration,

When i am forcefully sets layout_height/width of Surfaceview in XML, video displayed fine. surface_holder.setFixedSize(w,h) has no effect, neither mp.setdisplay().

Please guide in this.

UPDATE

XML flie

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

<SurfaceView 
        android:id="@+id/surface" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="10dip" />
</framelayout>

MediaPlayer usage is as per following link

http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/

Thanks in advance.

3 Answers 3

79

Setting your SurfaceView layout to wrap_content will not size a video to play at the proper aspect ratio.

  • A SurfaceView is an optimized drawing surface
  • A video is drawn to a SurfaceView, not contained within it

wrap_content is synonymous with fill_parent for a SurfaceView.

What you want to do is get the dimensions of your video from the MediaPlayer object. You can then set the aspect ratio of the SurfaceView to match the video.

Some Basic initialization

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

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

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

Then the good stuff. I have omitted error checking here to reduce code, MediaPlayer calls should be wrapped in a try{}.

@Override
public void surfaceCreated(SurfaceHolder holder) {

    mp.setDataSource("/sdcard/someVideo.mp4");
    mp.prepare();

    //Get the dimensions of the video
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();

    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    //Commit the layout parameters
    mSurfaceView.setLayoutParams(lp);        

    //Start video
    mp.start();
}

Note that this code makes some assumptions about the dimensions of your video. As-is, it maximizes the width and assumes that the height is not greater than the height of the screen.

You may want to fit height instead of width, also you could check the dimension calculation and ensure that it is not greater than the screen or screen - other_layout_elements.

4
  • Note: the getVideoWidth and getVideoHeight are reversed in the above example
    – coco
    Commented Jul 9, 2012 at 14:14
  • Thanks, fixed in latest revision.
    – Error 454
    Commented Jul 9, 2012 at 17:58
  • 2
    hi i've got only a black screen with sound with this sample... what can i do to fix it?
    – David
    Commented Jul 25, 2013 at 9:59
  • well,"wrap_content is synonymous with fill_parent for a SurfaceView."This may not be true when setting "height" to "wrap_content" or "fill_parent" in XMlL and using code below.When using surfaceHolder.setFixedSize not mSurfaceView.setLayoutParams it works well.
    – aboutqx
    Commented Feb 6, 2016 at 7:00
5

here is the code I currently use in a project:

private MediaPlayer mMediaPlayer;
private SurfaceView mSurfaceView;
private SurfaceHolder holder;
private int mPos = 0;

...

int width = mSurfaceView.getWidth();
int height = mSurfaceView.getHeight();
float boxWidth = width;
float boxHeight = height;

float videoWidth = mMediaPlayer.getVideoWidth();
float videoHeight = mMediaPlayer.getVideoHeight();

Log.i(TAG, String.format("startVideoPlayback @ %d - video %dx%d - box %dx%d", mPos, (int) videoWidth, (int) videoHeight, width, height));

float wr = boxWidth / videoWidth;
float hr = boxHeight / videoHeight;
float ar = videoWidth / videoHeight;

if (wr > hr)
    width = (int) (boxHeight * ar);
else
    height = (int) (boxWidth / ar);

Log.i(TAG, String.format("Scaled to %dx%d", width, height));

holder.setFixedSize(width, height);
mMediaPlayer.seekTo(mPos);
mMediaPlayer.start();

the layout I'm using (you can just ignore the progress bar)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >
</SurfaceView>

2
  • please write full code... you haven't created MediaPlayer instance and haven't shown the relation between holder to media player. Commented Apr 28, 2015 at 10:20
  • I'm using VideoView now, it's a widget you can use it directly in the layout, it handles aspect ratio by itself
    – sherpya
    Commented May 3, 2015 at 19:39
-5

On your SurfaceView in XML are you using wrap_content ? This should fix your problem if not. You may need to paste a little more code for further investigation if that doesn't fix your problem.

Change your surface view width to wrap_content as well.

3
  • i am using wrap_content in XML, added source. Still video is kind of full screen, stretched, i want to display video in its original size.
    – JRC
    Commented Jan 29, 2011 at 8:43
  • I updated my answer, please change your width to wrap content as well. With either one of those set to fill it will cause the layout to stretch the video proportionately in either direction.
    – Joey
    Commented Feb 1, 2011 at 21:29
  • This is not an answer. It contains false information about SurfaceView and wrap_content.
    – Silex
    Commented May 15, 2015 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.