How resize TextureView to fullscreen when rotation 90? - android

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".

Related

Android Videoview Issue

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.

TextureVideo, media player and aspect ratio

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.

Android camera2 surface with size 1920x1080 is not valid

I've implemented camera2 in my app and it is working on Nexus 6P and Nexus 5. Now I'm trying to test it on other devices, and the first one I tried on failed straight away. This is the error I get on HTC M7 running Lollipop:
Surface with size (w=1920, h=1080) and format 0x1 is not valid,
size not in valid set: [1920x1088, 1440x1088, 1456x832, 1088x1088,
1280x720, 960x720, 960x544, 720x720, 800x480, 768x464, 720x480,
768x432, 640x480, 544x544, 576x432, 640x384, 640x368, 480x480,
480x320, 384x288, 352x288, 320x240, 240x160, 176x144]
Any suggestions what should I do in this case? I've tried calculating the nearest resolution to my TextureView (which is 1280x720) and resizing TextureView accordingly, but that doesn't look particularly nice - too much unused space... Didn't see this problem on this device using old camera and SurfaceView
EDIT:
The problem seems to be inside my TextureView. This is my code:
inside a controller I have:
TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
int width, int height) {
startLollipopPreview(width, height);
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
int width, int height) {
configureTransform(width,height);
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
private void startLollipopPreview(int width, int height) {
CameraProxy camera = getCurrentCamera();
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
mPreviewSize = camera
.getOptimalPreviewSize((Activity) mContext,
camera.getSupportedPreviewSizes(
width, height), (double) width / height);
mPreviewTexture.setAspectRatio(mPreviewSize.width, mPreviewSize.height);
mPreviewTexture.getSurfaceTexture().setDefaultBufferSize(mPreviewSize.width,
mPreviewSize.height);
configureTransform(mPreviewSize.width, mPreviewSize.height);
camera.setStateAndHandler(getCameraStateCallback(), mBackgroundHandler);
camera.open();
} catch (CameraHardwareException e) {
Log.e(TAG, ".surfaceCreated() - Error opening camera", e);
((Activity) mContext).finish();
} catch (InterruptedException e) {
Log.e(TAG, ".surfaceCreated() - InterruptedException opening camera", e);
}
}
configureTransform() looks exactly like the Camera2 google sample so I don't think the problem is in there.
inside my TextureView I have the following:
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
params.gravity = Gravity.CENTER_VERTICAL;
setLayoutParams(params);
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
I believe you need to set the buffer size to a supported preview size:
textureView.getSurfaceTexture().setDefaultBufferSize(1280,720);
and then you can scale the TextureView so that it fits your screen, even if the preview size is smaller. The Camera2Video sample has an example. Specifically look at configureTransform in Camera2VideoFragment:
/**
* Configures the necessary {#link android.graphics.Matrix} transformation to `mTextureView`.
* This method should not to be called until the camera preview size is determined in
* openCamera, or until the size of `mTextureView` is fixed.
*
* #param viewWidth The width of `mTextureView`
* #param viewHeight The height of `mTextureView`
*/
private void configureTransform(int viewWidth, int viewHeight) {
Activity activity = getActivity();
if (null == mTextureView || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
}
mTextureView.setTransform(matrix);
}
The problem you facing change according to emulator/device that you testing the app.
This error occurs when the devices compatible sizes for the dimension of the screen and the MediaRecorder settings are not matched.
When the camera is open (openCamera(int w, int h) method) it's getting the sizes of supported resolutions by using chooseOptimalSize(.... .....) method. But before that, it will set videoSize().
Try to Logcat to get what are the supported sizes and according to that set the mMediaRecorder.setVideoSize(w,h);

How to manage Custom Video player Zoom In / Zoom Out programmatically?

We can achieve CUSTOM video player through two way:
VideoView
SurfaceView and use MediaPlayer for doing this.
I read a tutorial relate to Custom Video Player in which SurfaceView and use MediaPlayer used for achieving this functionallty - - link here
What is my need:
How to make your custom view like "When you touch on the video screen such as zoom out/zoom in, video view goes to the full screen and after zoom in gesture back to fix size what earlier " ?
Could someone suggest me how to do this thing?
I have share my code with you all i am useing Texture view for zoom in/out using two finger (pinch Zoom)
package com.app.sampletextureview;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.SurfaceTexture;
import android.media.MediaActionSound;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import java.io.IOException;
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener,View.OnTouchListener {
private static final String TAG = MainActivity.class.getName();
private static final String FILE_NAME = "Vid.mp4";
private MediaPlayer mMediaPlayer;
private TextureView mTextureView;
private RelativeLayout mrRelativeLayout;
private float mVideoWidth;
private float mVideoHeight;
DisplayMetrics dm;
RelativeLayout.LayoutParams params;
private ScaleGestureDetector scaleGestureDetector;
private Matrix matrix = new Matrix();
#SuppressWarnings("unused")
private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;
// These matrices will be used to scale points of the image
Matrix savedMatrix = new Matrix();
// The 3 states (events) which the user is trying to perform
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// these PointF objects are used to record the point(s) the user is touching
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
private float mVideoWidth;
private float mVideoHeight;
int viewWidth=0,viewHeight=0,xoff=0,yoff=0;;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculateVideoSize();
initView();
}
private void initView() {
mrRelativeLayout= (RelativeLayout) findViewById(R.id.rootView);
mTextureView = (TextureView) findViewById(R.id.textureView);
mTextureView.setOnTouchListener(this);
/*Log.d("Height-WidthOnCreate",""+mVideoHeight+"-"+mVideoWidth);
mTextureView.setMinimumWidth((int)mVideoWidth);
mTextureView.setMinimumHeight((int)mVideoHeight);
mTextureView.setSurfaceTextureListener(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
// don't forget to call MediaPlayer.prepareAsync() method when you use constructor for
// creating MediaPlayer
mMediaPlayer.prepareAsync();
adjustAspectRatio((int) mVideoWidth, (int)mVideoHeight);
Log.d("SurfaceCreate",""+mVideoWidth+""+mVideoHeight);
// 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());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
private void calculateVideoSize() {
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(
afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
String height = metaRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = metaRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
mVideoHeight = Float.parseFloat(height);
mVideoWidth = Float.parseFloat(width);
Log.d("Height-Width",""+mVideoHeight+"-"+mVideoWidth);
} catch (IOException e) {
Log.d(TAG, e.getMessage());
} catch (NumberFormatException e) {
Log.d(TAG, e.getMessage());
}
}
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);
}
private void adjustAspectRatio(int videoWidth, int videoHeight) {
int viewWidth = mTextureView.getWidth();
int viewHeight = mTextureView.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();
mTextureView.getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
//txform.postRotate(10); // just for fun
txform.postTranslate(xoff, yoff);
mTextureView.setTransform(txform);
}
/* #Override
public boolean onTouchEvent(MotionEvent ev) {
// scaleGestureDetector.onTouchEvent(ev);
return true;
}
*/
private class ScaleListener extends ScaleGestureDetector.
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = detector.getScaleFactor();
scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));
matrix.setScale(scaleFactor, scaleFactor);
Log.d("Matrix", "" + scaleFactor);
mTextureView.setTransform(matrix);
return true;
}
}
#Override
public boolean onTouch(View v, MotionEvent event)
{
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN: // first finger down only
if (!istouch) {
istouch=true;
ll_bottombar.setVisibility(View.VISIBLE);
ll_topheader.setVisibility(View.VISIBLE);
hideSystemUI(getWindow());
}else{
String tag = btnLock.getTag().toString();
if (tag.equals("false")) {
istouch = false;
ll_bottombar.setVisibility(View.GONE);
ll_topheader.setVisibility(View.GONE);
hideSystemUI(getWindow());
}
}
break;
case MotionEvent.ACTION_UP: // first finger lifted
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_UP: // second finger lifted
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down
//case MotionEvent.ACTION_POINTER_2_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 5f) {
savedMatrix.set(matrix);
midPoint(mid, event);
// matrix.setScale( mid.y,(float) newWidth / viewWidth,mid.x, (float) newHeight / viewHeight);
// matrix.setScale(mid.x,mid.y);
if(event.getPointerCount() >= 2) {
event.setAction(MotionEvent.ACTION_MOVE);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == NONE)
{
mode = NONE;
Log.d(TAG, "mode=NONE");
}
if (event.getPointerCount() >= 2) {
/* if (mode == DRAG)
{
// matrix.set(savedMatrix);
// matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix of points
}else if (mode == NONE)
{
mode = NONE;
Log.d(TAG, "mode=NONE");
}*/
if (mode == ZOOM) {
// pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f) {
matrix.set(savedMatrix);
Log.d(TAG, "NEW="+newDist+"--"+oldDist);
Log.d(TAG, "NEW Mid Point="+mid.x+"--"+mid.y);
scale = newDist / oldDist;
// matrix.setScale();
// matrix.setScale(scale,scale);
matrix.postScale(scale,scale,mid.x, mid.y);
Log.d(TAG,"New Hight Width"+mTextureView.getWidth()+"--"+mTextureView.getHeight());
}
}
mTextureView.setTransform(matrix);
}
break;
}
// display the transformation on screen
return true; // indicate event was handled
}
/* * --------------------------------------------------------------------------
* Method: spacing Parameters: MotionEvent Returns: float Description:
* checks the spacing between the two fingers on touch
* ----------------------------------------------------
*/
private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float)Math.sqrt(x * x + y * y);
}
/* * --------------------------------------------------------------------------
* Method: midPoint Parameters: PointF object, MotionEvent Returns: void
* Description: calculates the midpoint between the two fingers
* ------------------------------------------------------------
private void midPoint(PointF point, MotionEvent event)
{
float x = mrRelativeLayout.getX()+mrRelativeLayout.getWidth();
float y = mrRelativeLayout.getY()+mrRelativeLayout.getHeight();
point.set(x / 2, y / 2);
}
private void dumpEvent(MotionEvent event)
{
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE","POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
{
sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++)
{
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
}
private void adjustAspectRatio(int videoWidth, int videoHeight) {
viewWidth = mTextureView.getWidth();
viewHeight = mTextureView.getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
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;
}
xoff = (viewWidth - newWidth) / 2;
yoff = (viewHeight - newHeight) / 2;
Log.d(TAG, "video=" + videoWidth + "x" + videoHeight +
" view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight +
" off=" + xoff + "," + yoff);
Log.d(TAG, "video="+mTextureView.getHeight()+"--"+mTextureView.getWidth());
Log.d(TAG, "video="+newHeight+"--"+newWidth);
mTextureView.setMinimumWidth(newWidth);
mTextureView.setMinimumHeight(newHeight);
// Matrix txform = new Matrix();
mTextureView.getTransform(matrix);
matrix.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
//txform.postRotate(10); // just for fun
matrix.postTranslate(xoff, yoff);
mTextureView.setTransform(matrix);
}
}
}

Issue when playing video in local and url path

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);
}

Categories

Resources