Skip to content Skip to sidebar Skip to footer

Scale Video In Android's VideoVideo

I know how to strech ViewVideo to fit screen. But MX Player permits to make video bigger. Video is virtually bigger then screen. So viewer will see cropped part of video. Is it po

Solution 1:

I dont think you can do it with just a regular VideoView... although I was able to do that with a bit more complicated approach.

TLDR: You need to create a custom view that would act similarly to VideoView but is based on TextureView instead of SurfaceView and set proper transform matrix for this view.

TLDR EDIT: See the edit of my answer first, because you might actually be able to do that with VideoView...

Creating this view is a bit tricky mostly due to the fact that MediaPlayer events can be triggered in different order on different devices and when playing different videos.

The basic idea would be:

  • create a custom View that extends TextureView
  • keep a MediaPlayer object that would manage whole playing video thing... as for example a field of the view
  • set your view's TextureListener with setSurfaceTextureListener()
  • inside TextureListener methods (inside onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) to be more specific) create a new Surface with constructor:

Surface surface = new Surface(surfaceTexture);
  • and set this surface as a surface of the MediaPlayer with:

mMediaPlayer.setSurface(surface);

And the creation of this transform matrix could look like this (the example is from the old code of mine and if I remember correctly it was fitting the video to the height of the view and cropping everything that was left on sides):

float scaleY = 1.0f;
float scaleX = (mVideoWidth * mViewHeight / mVideoHeight) / mViewWidth;

int pivotPointX = (int) (mViewWidth / 2);
int pivotPointY = (int) (mViewHeight / 2);

Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);

setTransform(matrix);

EDIT:

Well... when I think about it one more time, maybe you could actually do that with just a regular VideoView. Just follow the similar approach with the scaleX,scaleY,pivotPointX and pivotPointY calculation and set those values for the underlying SurfaceView of the VideoView with methods setScaleX(), setScaleY(), setPivotX() and setPivotY() and see if it works. And let me know if it works if you test it.


Post a Comment for "Scale Video In Android's VideoVideo"