Extending MediaController for android

泄露秘密 提交于 2019-12-17 16:29:49

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7881272/extending-mediacontroller-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!