扩展的MediaController为AndroidMediaController、Android

2023-09-12 09:53:23 作者:Echo(回音)

我是新来的Java所以裸陪我。我使用的是 VideoView 的MediaController 一个应用程序我工作。我只是想有的MediaController 出现在顶我的 VideoView 但显然你不能这样做很容易。我试图使用 setAnchorView 的方法来我 VideoView ID,但没有奏效。无论我做什么,在的MediaController 总是在我的屏幕的底部。

I am new to java so bare with me. 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.

随着中说,我做了一些研究,它看起来好像我去延长的MediaController ,我可以改变位置和其他属性。我创建了一个新的类:

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);
    }
}

和我的父类:

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);
}

据上述工作时,我的默认的MediaController 仍然弹出,并拥有所有相同的功能。现在的问题是,我该如何去实际重新定位控制器从里面我 VFPlayer 类?

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?

推荐答案

在preferred办法控制的MediaController的位置是通过 setAnchorView 。不幸的是,VideoView似乎重写此值每当一个新的视频被加载成为自身的父视图。为了应对这种行为,一类像下面应该工作

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
    }
}

一旦你这样做,你可以用这个的MediaController设置锚的踪影查看您的愿望,而不必担心你的VideoView改变它。在此之后,获得一个观点,在正确的地方,其只是一个问题,以固定它。

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.