3

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?

KKO
  • 1,913
  • 3
  • 27
  • 35
  • btw, the setZOrderOnTop is there because I have a textview on top of the videoview, so that it shows some writing before I start the movie – KKO May 21 '13 at 22:19
  • Get an answer here: [MediaController positioning over VideoView][1] Good luck! [1]: http://stackoverflow.com/q/3686729/1832221 – lopez.mikhael May 21 '13 at 22:37
  • in that example, they do it like i did it the first time, not with xml – KKO May 22 '13 at 06:13

2 Answers2

10

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

Ronald
  • 417
  • 5
  • 11
0

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

Ritz
  • 1
  • I've read that, it's how i did it the first time. And i've tried with setAnchorView in my second example, but still didn't work – KKO May 22 '13 at 06:15