I have a VideoView that takes up the top half of the Activity in portrait orientation with the bottom half of the screen showing some images and text. I am playing a rtsp v
I found really easy solution.
Just wrap the videoView in a FrameLayout then you can add the MediaController to that FrameLayout from code, like this:
MediaController mc = new MediaController(context);
videoView.setMediaController(mc);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
mc.setLayoutParams(lp);
((ViewGroup) mc.getParent()).removeView(mc);
((FrameLayout) findViewById(R.id.videoViewWrapper)).addView(mc);
EDIT: since I posted this answer I ran into a lot of issues with getting it to hide and with controlling its size so what I ended up doing was I just created my own layout with controls that I could animate and work with without headaches
I use this code to solve it.
mediaController.setPadding(0, 0, 0, px);
to set the mediacontroller view to the position you want. Hope this help you.
I encounter the same problem recently, using setAnchorView(videoView) will set the controller fully under VideoView instead hovering on the bottom area of it. My VideoView is one third screen in upper area, so the controller end up covering whatever View under VideoView.
Below is the way I end up doing it without writing full-blown custom controller (only overriding onSizeChanged
of MediaController to move anchor up) :
Use FrameLayout as an anchor for MediaContoller, wrap it up together with VideoView as below :
<RelativeLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.3"
android:layout_gravity="center"
android:background="#000000">
<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<FrameLayout android:id="@+id/controllerAnchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Create custom MediaController that will move FrameLayout up (with MediaController that is anchored to it will follow) when its size changed :
public class MyMediaController extends MediaController
{
private FrameLayout anchorView;
public MyMediaController(Context context, FrameLayout anchorView)
{
super(context);
this.anchorView = anchorView;
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) anchorView.getLayoutParams();
lp.setMargins(0, 0, 0, yNew);
anchorView.setLayoutParams(lp);
anchorView.requestLayout();
}
}
Use the custom controller above in place of the standard one and then anchor it to the FrameLayout :
protected void onCreate(Bundle savedInstanceState)
{
//...
videoView = (VideoView) findViewById(R.id.videoView1);
videoController = new MyMediaController(this, (FrameLayout) findViewById(R.id.controllerAnchor));
videoView.setMediaController(videoController);
//...
}
public void onPrepared(MediaPlayer mp)
{
videoView.start();
FrameLayout controllerAnchor = (FrameLayout) findViewById(R.id.controllerAnchor);
videoController.setAnchorView(controllerAnchor);
}
Put your video view inside a linear layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.videoplayer.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<VideoView
android:layout_width="300dp"
android:layout_height="180dp"
android:id="@+id/player"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>