I play the video in VideoView
When I play from SD Card, it shows correct width and height in full screen. When I try to play from server, video Width is smaller and the video is re-sized.
How can I play the video in original size and full screen?
Take a look at the documentation and implement the onPrepared method like the following code:
//prepare the video
public void onPrepared(MediaPlayer mp) {
progressBarWait.setVisibility(View.GONE);
//First get the size of the video
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
//get the size of the screen
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
//Adjust
LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
surfaceViewFrame.setLayoutParams(lp);
if (!player.isPlaying()) {
player.start();
}
surfaceViewFrame.setClickable(true);
}
Related
I using a TextureView to play video:
I have to use rotation=90 to display video correct orientation when record video
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777777" >
<TextureView
android:id="#+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="90"
android:layout_alignParentTop="true" />
i try set setTransform of TextureView to resize video to fullsreen, but it not working:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(FILE_NAME);
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(false);
mMediaPlayer.prepareAsync();
updateTextureViewScaling(width,height);
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
private void updateTextureViewScaling(int viewWidth, int viewHeight) {
float scaleX = 1.0f;
float scaleY = 1.0f;
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int VIDEO_HEIGHT = displayMetrics.heightPixels;
int VIDEO_WIDTH = displayMetrics.widthPixels;
if (VIDEO_WIDTH > viewWidth && VIDEO_WIDTH > viewHeight) {
scaleX = (float) VIDEO_WIDTH / viewWidth;
scaleY = (float) VIDEO_HEIGHT / viewHeight;
} else if (VIDEO_WIDTH < viewWidth && VIDEO_HEIGHT < viewHeight) {
scaleY = (float) viewWidth / VIDEO_WIDTH;
scaleX = (float) viewHeight / VIDEO_HEIGHT;
} else if (viewWidth > VIDEO_WIDTH) {
scaleY = ((float) viewWidth / VIDEO_WIDTH) / ((float) viewHeight / VIDEO_HEIGHT);
} else if (viewHeight > VIDEO_HEIGHT) {
scaleX = ((float) viewHeight / VIDEO_WIDTH) / ((float) viewWidth / VIDEO_WIDTH);
}
// Calculate pivot points, in our case crop from center
int pivotPointX = viewWidth / 2;
int pivotPointY = viewHeight / 2;
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);
textureView.setTransform(matrix);
}
Target result:
Image 1: not set android:rotation="90"
Image 2: set android:rotation="90"
How can set width and height of video to fullscreen:
i had log:
Width and Height in onSurfaceTextureAvailable = size of Screen.
My problem had fixed by:
put TextureView in a FrameLayout
and update function updateTextureViewScaling
private void updateTextureViewScaling(int viewWidth, int viewHeight) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) textureView.getLayoutParams();
params.width = viewHeight;
params.height =viewWidth;
params.gravity= Gravity.CENTER;
textureView.setLayoutParams(params);
}
Overide this method in Activity:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
textview.setRotation(90);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
textview.setRotation(0);
}
}
in Android Manifest file mention this under activity
android:configChanges="orientation|screenSize".
I want to run video in full screen in portrait mode. I m able to run video in full screen but my video is stretching. I am using videoview. And tried TextureView/SurfaceView but no luck.
I am unable to maintain the aspect ratio of video.
Below is my code:
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceHolder = binding.video.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), mUri);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// surfaceHolder.setFixedSize(surfaceView_Width,surfaceView_Height);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
/*
* Handle aspect ratio
*/
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
int surfaceView_Width = binding.video.getWidth();
int surfaceView_Height = binding.video.getHeight();
float video_Width = mediaPlayer.getVideoWidth();
float video_Height = mediaPlayer.getVideoHeight();
float videoProportion = (float) video_Width / (float) video_Height;
LayoutParams layoutParams = binding.video.getLayoutParams();
if (videoProportion > screenProportion) {
layoutParams.width = screenWidth;
layoutParams.height = (int) ((float) screenWidth / videoProportion);
} else {
layoutParams.width = (int) (videoProportion * (float) screenHeight);
layoutParams.height = screenHeight;
}
binding.video.setLayoutParams(layoutParams);
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mediaPlayer.setDisplay(surfaceHolder);
}
Please help me with this. Thanks in advance.
I have a TextureView in which a video is showing, a video previously recorded with device camera. The image in the video is stretched, and that is not acceptable. I did some research, and found this method:
private void adjustAspectRatio(int videoWidth, int videoHeight) {
int viewWidth = m_TextureView.getWidth();
int viewHeight = m_TextureView.getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
int newWidth, newHeight;
if (viewHeight > (int) (viewWidth * aspectRatio)) {
// limited by narrow width; restrict height
newWidth = viewWidth;
newHeight = (int) (viewWidth * aspectRatio);
} else {
// limited by short height; restrict width
newWidth = (int) (viewHeight / aspectRatio);
newHeight = viewHeight;
}
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
Log.v(TAG, "video=" + videoWidth + "x" + videoHeight +
" view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight +
" off=" + xoff + "," + yoff);
Matrix txform = new Matrix();
m_TextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
//txform.postRotate(10); // just for fun
txform.postTranslate(xoff, yoff);
m_TextureView.setTransform(txform);
}
Tried to use it, but I see a black background only.
This is mu onSurfaceTextureAvailable, in case it helps:
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
isVideoResultReady=true;
Surface s=new Surface(surface);
if(mPlayer==null){
mPlayer=new MediaPlayer();
}
try {
//mPlayer.setDataSource(videoPath);
mPlayer.setSurface(s);
mPlayer.prepare();
mPlayer.setOnBufferingUpdateListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnPreparedListener(this);
//media player is started in other point of the app
} catch (IOException e) {
e.printStackTrace();
}
}
What is wrong with that method? How to keep aspect ratio? Thanks.
Need to resize a image(bitmap) without losing quality and clearness as per aspect ratio. I see that some time lose quality and some time lose size. I want a good quality of image after resize in mayn't exactly required height and width. I have use :
`
private Bitmap scaleToActualAspectRatio(Bitmap image) {
int maxWidth = getActivity().getWindowManager().getDefaultDisplay()
.getWidth();
int maxHeight = getActivity().getWindowManager().getDefaultDisplay()
.getHeight();
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = (int) ((float)maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float)maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}`
Is there any way to zoom the video from any specific coordinate on touch. I have tried all the way from mediaplayer and surfaceview and making both custom and accessing window manager and all.If any one has idea so please explain.
You can do this using SurfaceView. Check out my article Surface View - Video Cropping.
Code to crop SurfaceView.
private void updateTextureViewSize(int viewWidth, int viewHeight) {
float scaleX = 1.0f;
float scaleY = 1.0f;
if (mVideoWidth > viewWidth && mVideoHeight > viewHeight) {
scaleX = mVideoWidth / viewWidth;
scaleY = mVideoHeight / viewHeight;
} else if (mVideoWidth < viewWidth && mVideoHeight < viewHeight) {
scaleY = viewWidth / mVideoWidth;
scaleX = viewHeight / mVideoHeight;
} else if (viewWidth > mVideoWidth) {
scaleY = (viewWidth / mVideoWidth) / (viewHeight / mVideoHeight);
} else if (viewHeight > mVideoHeight) {
scaleX = (viewHeight / mVideoHeight) / (viewWidth / mVideoWidth);
}
// Calculate pivot points, in our case crop from center
int pivotPointX = viewWidth / 2;
int pivotPointY = viewHeight / 2;
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);
mTextureView.setTransform(matrix);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
}