为 android 扩展 MediaController

发布于 2024-12-11 10:00:34 字数 1106 浏览 2 评论 0原文

我正在为我正在开发的应用程序使用 VideoViewMediaController 。我只是想让 MediaController 出现在我的 VideoView 之上,但显然你不能很容易地做到这一点。我尝试对我的 VideoView id 使用 setAnchorView 方法,但这不起作用。无论我做什么,MediaController 始终位于屏幕底部。

话虽如此,我做了一些研究,看起来好像我要扩展 MediaController,我可以更改位置和其他属性。我创建了一个新类:

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 类中实际重新定位控制器?

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2024-12-18 10:00:35

控制 MediaController 位置的首选方法是通过 setAnchorView。不幸的是,每当加载新视频时,VideoView 似乎都会将此值覆盖为自身的父视图。为了解决这个问题,像下面这样的类应该可以工作

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 将 Anchor 设置为你想要的任何视图,而不必担心你的 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
    }
}

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.

姜生凉生 2024-12-18 10:00:35

您应该能够重写

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

VFPlayer 类中的方法。从这里将视图/小部件放在您想要的位置。

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.

嘦怹 2024-12-18 10:00:35

几个月前我对 Android 的了解是这样的:

当困难来临时,制作自己的组件

我的意思是,很容易让你的组件布局,宽度为4或5个ImageButtons/Buttons,并将它们链接到VideoView组件功能(或SurfaceView以获得更好的控制)。

一些VideoView函数(摘自此处):

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

然后你可以一些带有按钮的布局,以便将它们放在您想要的位置,而不会出现尝试扩展像 V 这样的刚性组件的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文