Android media player set video scaling mode
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_CROPPING
in 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
}
Shockelduck
Updated on June 04, 2022Comments
-
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 over 9 yearsDid you find out the answer?
-
-
mbonnin about 8 yearsFYI,
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 almost 6 yearsIf you wish, and use it in normal Activity/Fragment, I've shown how to scale videos on ExoPlayer here: stackoverflow.com/a/51231575/878126