0

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"

enter image description here

Image 2: set android:rotation="90"

enter image description here

How can set width and height of video to fullscreen: i had log: Width and Height in onSurfaceTextureAvailable = size of Screen.

D T
  • 3,522
  • 7
  • 45
  • 89

2 Answers2

4

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);
}
D T
  • 3,522
  • 7
  • 45
  • 89
0

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".
sajad zohrei
  • 377
  • 1
  • 10
  • it not ok. i not want rotation screen when change phone. android:screenOrientation="portrait" – D T Sep 06 '18 at 08:35
  • My problem: if i not set android:rotation="90", video display rotation 90 (image 1). if i set android:rotation="90", size of video display the image 2. i want image 2 , it can fullscreen the same image 1. – D T Sep 06 '18 at 08:36
  • Must i set android:rotation="90" in Layout? – D T Sep 06 '18 at 08:43
  • your code only rotation textview , not set full screen video after rotation. – D T Sep 06 '18 at 08:48
  • Now, i had rotation ok by: android:rotation="90", My problem is full screen for video(Height of video =Height of Screen). – D T Sep 06 '18 at 08:49
  • Do you want to media player full screen in the portrait mode? – sajad zohrei Sep 06 '18 at 09:09
  • Check out https://stackoverflow.com/a/19448550/5027107 You have to crop-center it – sajad zohrei Sep 06 '18 at 09:14
  • Can't set fullscreen? – D T Sep 06 '18 at 10:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179665/discussion-between-sajad-zohrei-and-d-t). – sajad zohrei Sep 08 '18 at 04:08