Extending MediaController for android
Asked Answered
N

3

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

Nabataean answered 24/10, 2011 at 20:2 Comment(1)
I use this solution to change the MediaController postion. Hope this helps you.Leges
B
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.

Bhakti answered 27/10, 2011 at 15:43 Comment(12)
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 workingNabataean
What did happen? Also, to be sure, you must be creating your MediaController programmatically for that to work. Did you do that?Bhakti
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 toNabataean
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.Bhakti
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.Nabataean
Some of those variables are private. If you want to travel down that path, I would suggest downloading netmite.com/android/mydroid/2.0/frameworks/base/core/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.Bhakti
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 eclipseNabataean
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?Bhakti
let us continue this discussion in chatNabataean
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). code.google.com/p/android/issues/detail?id=21306Bhakti
I have done so.. But noting happens (no changes in output)... Or clearify in detail..Foch
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.Bhakti
J
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.

Juggler answered 27/10, 2011 at 15:30 Comment(1)
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 startNabataean
W
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.

Willable answered 27/10, 2011 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.