Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position Video Inside a VideoView

So I have extended VideoView's onMeasure to scale up the video to fit inside a fullscreen view.

here is how:

public void setVideoAspect(int w,int h){     wVideo=w;     hVideo=h;     onMeasure(w, h); }  @Override  protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)  {      super.onMeasure(widthMeasureSpec, heightMeasureSpec);      if(wVideo!=0 && hVideo!=0)      setMeasuredDimension(wVideo,hVideo);  } 

I call setVideoAspect() with the display metrics (width, hight) of the screen. The problem is that this method stretches the video to fit inside the screen. I want to be able to keep the aspect ratio. (I have 4:3 video and 3:2 screen size.) I used the folowing code to give the retained ratio measurements to the view:

int height =  (int) (metrics.widthPixels*3/(float)4);                         int width=  metrics.widthPixels;                            mVideoView.setVideoAspect(width,height); 

So this does the job but there is an issue: it gives me a 4:3 video with the width of the screen and scales the height correctly, but it doesn't center the video. (It just crops the bottom part of the video instead of the top and the bottom equally.) I have a relative layout containing the VideoView with the gravity of the VideoView set to center.

like image 223
DArkO Avatar asked Jan 06 '11 20:01

DArkO


People also ask

How to play video in center in VideoView in Android?

Try android:layout_centerInParent="true in Video View .

How is the video file handled in the Android application using Kotlin?

VideoView class of Kotlin is used to display video files in the android application. This class supports the 3gp and MP4 video formats. VideoView class is capable of playing a video file either from local storage, specific URL or from a resource file.


2 Answers

Try using a FrameLayout instead. I'm not sure why, but if I use a Linear or Relative in my code it won't center, but FrameLayout does. Here is the XML that fit my video to the screen, preserving the ratio and centering it:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@drawable/bg">     <!-- Video player -->     <VideoView         android:id="@+id/surface_view"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_gravity="center"/> </FrameLayout> 
like image 115
Cameron Avatar answered Sep 27 '22 19:09

Cameron


In order to center the video in the RelativeLayout I added both layout_gravity="center" ad layout_centerInParent="true". It works on my Android 4.3 phone.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">     <VideoView android:id="@+id/surface_view"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_gravity="center"         android:layout_centerInParent="true" /> </RelativeLayout> 
like image 30
frognosis Avatar answered Sep 27 '22 18:09

frognosis