Android media player set video scaling mode

12,852

VIDEO_SCALING_MODE_SCALE_TO_FIT is the value by default, so calling to

mediaPlayer.setVideoScalingMode(android.media.MediaPlayer
                                .VIDEO_SCALING_MODE_SCALE_TO_FIT);

will make no difference.

http://developer.android.com/reference/android/media/MediaCodec.html#setVideoScalingMode(int)

The default is "scale to fit".

As example, grab the following MediaCodec based player:

https://github.com/google/ExoPlayer

and experiment by setting VIDEO_SCALING_MODE_SCALE_TO_FIT or VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPINGin MediaCodecVideoTrackRenderer.java


Adding a bit more.

What you are trying to accomplish can be done the following way:

Implement the onMeasure on your SurfaceView.

Get the video size.

Call to setMeasuredDimension with different values depending if you want to Stretch, Fit or Crop your video:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(videoWidth, widthMeasureSpec);
        int height = getDefaultSize(videoHeight, heightMeasureSpec);

        // Do not change w & h for screen fill
        setMeasuredDimension(width, height);

        // Experiment by changing width & height compared to the video size
        // for different results
    }
Share:
12,852
Shockelduck
Author by

Shockelduck

Updated on June 04, 2022

Comments

  • Shockelduck
    Shockelduck about 2 years

    I use:

     mediaPlayer.setVideoScalingMode(android.media.MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
    

    but it is not working i want to fit video to screen.

    • Behnam
      Behnam over 9 years
      Did you find out the answer?
  • mbonnin
    mbonnin about 8 years
    FYI, VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING is not working right now. It's a known issue: github.com/google/ExoPlayer/issues/1428
  • android developer
    android developer almost 6 years
    If you wish, and use it in normal Activity/Fragment, I've shown how to scale videos on ExoPlayer here: stackoverflow.com/a/51231575/878126