KKO
KKO

Reputation: 1933

Android MediaController position

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?

Upvotes: 3

Views: 9590

Answers (2)

Ronald
Ronald

Reputation: 417

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).

Upvotes: 10

Ritz
Ritz

Reputation: 1

If you look at MediaController class (http://developer.android.com/reference/android/widget/MediaController.html), it states:

The way to use this class is to instantiate it programatically. The MediaController will create a default set of controls and put them in a window floating above your application. Specifically, the controls will float above the view specified with setAnchorView(). The window will disappear if left idle for three seconds and reappear when the user touches the anchor view.

You need to know the video size to place the mediacontroller appropriately by calling the setAnchorView() in OnVideoSizeChangeListener

Upvotes: 0

Related Questions