Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

subclass - Extending MediaController for android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...