MediaController positioning over VideoView

android videoview custom mediacontroller
videoview with seekbar android
android videoview play/pause button
android video view library
videoview not playing video in android
android mediacontroller change style
android media controller source code
videoview hide controls android

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 video stream in the video view when the Activity starts. I have attached a MediaController to the VideoView via the following code:

    MediaController controller = new MediaController(this);
    controller.setAnchorView(this.videoView);
    controller.setMediaPlayer(this.videoView);
    this.videoView.setMediaController(controller);

When I tap the VideoView to bring up the MediaController on the screen I expected the playback controls to appear overlaying the bottom area of the VideoView (the bottom of the MediaController even with the bottom of the VideoView). Instead the MediaController pops up lower down on the screen, overlaying some of the graphics and text I have below the VideoView.

Are there some additional steps I need to take to get the MediaController to appear where I want it to on the screen?

Frankly, I'd just write my own controller. In fact, I did once.

That being said, try setAnchorView() -- by my reading of the source code, the MediaController will appear at the bottom of whatever the anchor view is.

video-player-android/MediaController.java at master · appunite , The MediaController will create a default set of controls. * and put them in the control view. * This can for example be a VideoView, or your Activity's main view. I am using VideoView to show play a local mp4 and i am using MediaController as well. The media control bar is not displaying under my video clip but in the middle of the screen. I used setAnchorView to attach it to my videoView but that had no affect. How do i position my mediacontroller to be directly under my videoview?

Setting the anchor view will only work if the videoview size is known - it will not be upon init. But you can do something like this:

video.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() { 
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                /*
                 * add media controller
                 */
                mc = new MediaController(YourActivity.this);
                video.setMediaController(mc);
                /*
                 * and set its position on screen
                 */
                mc.setAnchorView(video);
            }
        });
    }
});

Java Code Examples android.widget.MediaController, myController.setAnchorView(myVideo); // To link the video view to the media controller. myVideo.setMediaController(myController); // To autoplay on opening. But i want to set margin of controller with screen,and i don't know how to set it,my Videoview display in top right screen but my mediacontroller not display below VideoView,it display in center :D – user1797447 Nov 4 '12 at 4:33

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

An Android Studio VideoView and MediaController Tutorial , The position of the controls is designated by anchoring the controller instance to a specific view in the user interface layout. Once attached and anchored, the  mediaController.setAnchorView(findViewById(R.id.main_audio_view)); Using this snippet we can re position the mediacontroller to be placed at a particular position based on the layout id or an view's id

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

MediaController, The MediaController will create a default set of controls and put them in a This can for example be a VideoView, or your Activity's main view. When I tap the VideoView to bring up the MediaController on the screen I expected the playback controls to appear overlaying the bottom area of the VideoView (the bottom of the MediaController even with the bottom of the VideoView). Instead the MediaController pops up lower down on the screen, overlaying some of the graphics and text I have below the VideoView.

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.

android - MediaController positioning over VideoView -, I have a video that takes the top half activity in the picture orientation in the bottom half of the screen, showing some pictures And when I start  MediaController is a ViewGroup that can be added programmatically to any layout to control a VideoView. Use MediaController.setMediaPlayer() to attach a media player such as a VideoView to the controller. Use VideoView.setMediaController() to attach a MediaController to the VideoView.

MediaController Positioning - binding to VideoView, I ended up doing a dirty hack I just bound the view to my videoView to achieve the desired behavior: public void onPrepared(MediaPlayer mp)  If a video is simply played using the VideoView class, the user will not be given any control over the playback, which will run until the end of the video is reached. This issue can be addressed by attaching an instance of the MediaController class to the VideoView instance.

VideoView Tutorial With Example In Android Studio, Learn android methods, Media Controller and more used in VideoView with example in In particular it does not restore the current play position and play state. MediaController is a class which is used to provide the controls for the video playback. If a video is simply played using the VideoView class then the user will not be given any control over the playback of the video which will run until the end of the video is reached. This issue can be addressed by attaching an instance of the MediaController class to the VideoView instance.

Android Video Streamimg with Android VideoView and , Android Video Streamimg with Android VideoView and MediaController In particular, it does not restore the current play state, play position, selected tracks,​  The reason for above scenario is the MediaController can’t get the dimensions of the VideoView until the video playback is started so the MediaController is positioned below the VideoView.For setting media controller inside the videoView, we need to set the anchorView inside OnPreparedListener.Now lets see the final output after code change.

Comments
  • I am interested in this as well. Did you ever figure it out?
  • Have a look at this question stackoverflow.com/questions/6093756/…
  • @mbaird if you still want this solved: show me your xml (:
  • I am desperate to figure this out. I posted a question yesterday about extending the MediaController: stackoverflow.com/questions/7881272/… . I've looked at your github code but wasn't able to figure it out. I think I need to override the setAnchorView method so I can begin to place the MediaController wherever I like. Is their a way to make it "float" and position it that way?
  • I want to add stickers to video, I have posted a detailed question stackoverflow.com/questions/43963522/…, my problem is to get the user input, as commonsware has stated, relative to the overlay image(position, scale, rotation) and background video
  • Link is not working please update.
  • @PratikButaniAndroidDev: At this point, I recommend using more modern solutions, such as ExoPlayer, rather than VideoVidew.
  • That's okay, I am using ExoPlayer but as per question, your answer doesn't have answer because of dead link
  • make sense to call setAnchorView() once view ready to start. i.e. call from onPrepared()
  • setAnchorView(video) uses parent of VideoView to overlay the media controls. If you have a video with fixed height make sure you put it inside a framelayout so that media controls are on top of the video.
  • @Prakash This is what I was looking for.. your comment was all i needed... Many thanks.