Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android media player set video scaling mode

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.

like image 360
Shockelduck Avatar asked Oct 14 '25 03:10

Shockelduck


1 Answers

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
    }
like image 199
Miguel Beltran Avatar answered Oct 17 '25 06:10

Miguel Beltran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!