5

I am using a VideoView and the MediaController for an app I am working on. I simply wanted to have the MediaController appear on top of my VideoView but apparently you can't do that very easily. I attempted to use the setAnchorView method to my VideoView id, but that didn't work. No matter what I do, the MediaController is always at the bottom of my screen.

With that said, I did some research and it looks as if I go about extending MediaController, I can change position and other properties. I have created a new class:

package com.dop.mobilevforum;

import android.content.Context;
import android.widget.MediaController;

public class VFPlayer extends MediaController
{
    public VFPlayer(Context context)
    {
        super(context);
    }
}

and in my parent class:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vforum);

    controller  = new VFPlayer(this);
    vidPlayer   = (VideoView) findViewById(R.id.vidPlayer);
    vidPlayer.setMediaController(controller);
}

It the above is working, my default MediaController still pops up and has all the same functionality. The question is now, how do I go about actually repositioning the controller from inside my VFPlayer class?

enter image description here

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • I use [this solution](http://stackoverflow.com/a/9563643/544419) to change the MediaController postion. Hope this helps you. – Rongan Mar 05 '12 at 08:40

3 Answers3

17

The preferred way to control the location of the MediaController is via setAnchorView. Unfortunately, the VideoView seems to override this value to be the parent view of itself whenever a new video is loaded. To counter act this, a class like the following should work

public class ConstantAnchorMediaController extends MediaController
{

    public ConstantAnchorMediaController(Context context, View anchor)
    {
        super(context);
        super.setAnchorView(anchor);
    }

    @Override
    public void setAnchorView(View view)
    {
        // Do nothing
    }
}

Once you do that, you can use this MediaController to set the Anchor to whichever view you desire without worrying about your VideoView changing it. After that, its just a matter of getting a view in the right place to anchor it too.

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • I tried what you suggested in your second paragraph. I made a 1x1 dip View and placed it at the very top of my screen and tried the setAnchorView method to that and it didn't end up working – Ronnie Oct 27 '11 at 18:03
  • What did happen? Also, to be sure, you must be creating your MediaController programmatically for that to work. Did you do that? – Justin Breitfeller Oct 27 '11 at 18:34
  • I am creating it programatically. Nothing different happened. It positions itself right below the video. I swear, setAnchorView has made no difference no matter what view I set it to – Ronnie Oct 27 '11 at 18:38
  • Are you sure the view was positioned where you wanted it to be? I am just trying to cover all the bases. The other thing you can do is what I mentioned in the first paragraph. Override the show() method and play with the layout parameters to position the controller where you want it. If you set p.y = 0 instead of what I posted, it should put the controller at the top of your screen. – Justin Breitfeller Oct 27 '11 at 18:42
  • yeah I am positive. When I look at the xml in graphical view, its at the top of the screen. If I copy the show method above and paste it in my VFPlayer.java, I have a bunch of errors like unresolved variables and stuff. I thought when you extend something you automatically have access to everything that you're extending. I added an image of what I am seeing as of now. – Ronnie Oct 27 '11 at 18:52
  • Some of those variables are private. If you want to travel down that path, I would suggest downloading http://www.netmite.com/android/mydroid/2.0/frameworks/base/core/java/android/widget/MediaController.java and then modifying it to be the way you need it to be. That will last into the future much better if you can't figure out the anchor. You could even add that class and debug what was going on when you used setAnchor if you so choose. – Justin Breitfeller Oct 27 '11 at 18:58
  • Once I download the MediaController.java, do I replace the existing then modify it? I tried just copying all the contents into VFPlayer.java but getting some errors in eclipse – Ronnie Oct 27 '11 at 19:28
  • Well I am hoping you can figure most of those out yourself. There is an error with the isDown() function that needs to be replaced with event.getAction() == KeyEvent.ACTION_DOWN . The rest are missing resources that you can replace / download. The only one is PolicyManager makeNewWindow.. I'm not sure what you can do about that. You may be stuck there? – Justin Breitfeller Oct 27 '11 at 19:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4555/discussion-between-ronnie-and-justin-breitfeller) – Ronnie Oct 27 '11 at 19:36
  • Just an FYI, I posted a bug report about how the MediaController may be positioning itself incorrectly. I think the intent was for the MediaController to be positioned at the very bottom of its anchor (but still on top of it). http://code.google.com/p/android/issues/detail?id=21306 – Justin Breitfeller Oct 27 '11 at 21:58
  • I have done so.. But noting happens (no changes in output)... Or clearify in detail.. – Arpit Garg Nov 10 '11 at 11:38
  • It matters what you anchor your media controller to Arpit. As an example, try putting a LinearLayout before your VideoView and anchoring your media controller to that. It should show up at that point. Otherwise, I'd suggest you ask another questions so you can be helped. – Justin Breitfeller Nov 10 '11 at 18:38
1

You should be able to override the

android.view.View.OnLayout(boolean, int, int, int, int)

method in your VFPlayer class. From here put the view/widget where you want it.

zdp
  • 119
  • 4
  • can you elaborate on this? If I can override that method in my VFPlayer.java, that'd be nice, but I have no idea where to even start – Ronnie Oct 27 '11 at 18:41
1

What i have learning some months ago about Android is this :

When difficultys come , make your own components .

What i mean , that is easy to make your components layout , width 4 or five ImageButtons/Buttons , and link them to the VideoView component functions (or SurfaceView for a better control ).

Some of the VideoView functions (extracted from here):

start() -> To start a video play
pause() -> To pause it
seekTo(int msec) -> To go the exactly place , using it with a SeekBar
...

Then you can make some Layout with the buttons in order to put them where you want , without the problems of trying to extend a rigid component like V.

A.Quiroga
  • 5,704
  • 6
  • 37
  • 58