Harshil Dholakiya
Harshil Dholakiya

Reputation: 105

how to get width and height of videoview after video added

layout xml of videoview :

   <RelativeLayout
        android:id="@+id/videoEditorParent"
        android:layout_width="match_parent"
        android:layout_height="400dp">

        <RelativeLayout
            android:id="@+id/vidEditorWrapper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@color/colorAccent">

            <VideoView
                android:id="@+id/vidEditor"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </RelativeLayout>

    </RelativeLayout>

when i get height and width of video view it gives me match_parent's width and height, but not aspect ratio width and height of video is playing in videoview(whatever width and height is covered in videoview by video).

here, my code :

            viewWidth = vidEditor.getWidth();
            viewHeight = vidEditor.getHeight();

            ViewGroup.LayoutParams layoutParams = vidEditorWrapper.getLayoutParams();

            dpWidthView = utilities.pxToDp(viewWidth);
            dpHeightView = utilities.pxToDp(viewHeight);

            dpWidthViewPerVal = (dpWidthView * 2) / 100;

            layoutParams.width = viewWidth;
            layoutParams.height = viewHeight;
            vidEditorWrapper.setLayoutParams(layoutParams);

my screen shot :

enter image description here

i want width and height of black area that is video playing in videoview. but getWidth() or getMeasuredWidth() and getHeight() or getMeasuredHeight() of videoview gives me match_parent width and height.

so, how can i get width and height of video area(only dark area that is visible to us in picture not full width) that is covered in videoview.

Upvotes: 1

Views: 2641

Answers (1)

Arun Sivanandan
Arun Sivanandan

Reputation: 79

Try applying your code inside OnPreparedListener For eg:

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            viewWidth = vidEditor.getWidth();
            viewHeight = vidEditor.getHeight();

            ViewGroup.LayoutParams layoutParams = 
            vidEditorWrapper.getLayoutParams();

            dpWidthView = utilities.pxToDp(viewWidth);
            dpHeightView = utilities.pxToDp(viewHeight);

            dpWidthViewPerVal = (dpWidthView * 2) / 100;

            layoutParams.width = viewWidth;
            layoutParams.height = viewHeight;
            vidEditorWrapper.setLayoutParams(layoutParams);
        }
    });

Upvotes: 1

Related Questions