Android MediaController position

KKO picture KKO · May 22, 2013 · Viewed 9.1k times · Source

So I have the following code which works ok:

VideoView videoView = (VideoView)findViewById(R.id.videoView1);
videoView.setVideoPath("android.resource://" + getPackageName() + "/raw/"+R.raw.intro);

MediaController controller = new MediaController(this);
controller.setAnchorView(videoView);
controller.setPadding(0, 0, 0, 500);
videoView.setMediaController(controller);

videoView.setZOrderOnTop(true);

However, if I test the app on a phone with a smaller screen, the MediaController is positioned like s**t. So I tried to define it in the xml file so it keeps the same postion on different devices

<MediaController
        android:id="@+id/mediaController1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/videoView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" >
    </MediaController>

and changed the code to

VideoView videoView = (VideoView)findViewById(R.id.videoView1);
videoView.setVideoPath("android.resource://" + getPackageName() + "/raw/"+R.raw.intro);

MediaController controller = (MediaController)findViewById(R.id.mediaController1);
videoView.setMediaController(controller);

videoView.setZOrderOnTop(true);

But now, the app crashes when I start it. Is there something I'm missing here? How can I use the MediaController defined in the XML?

Answer

Ronald picture Ronald · Aug 27, 2013

When using a VideoView with setAnchorView, it will use the VideoView's parent as the anchor. http://developer.android.com/reference/android/widget/MediaController.html#setAnchorView(android.view.View)

So if you wrap you VideoView inside a FrameLayout, the Mediacontroller will be positioned better. My guess is that it is now anchored to some LinearLayout with different dimensions than your videoview (which only became clear on a small screen).