安卓:隐藏媒体控制器功能[英] android: hiding media controller functions

本文是小编为大家收集整理的关于安卓:隐藏媒体控制器功能的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个VideoView,当视频开始时,媒体控制器显示为3秒钟.除非我点击屏幕,否则我想隐藏媒体控制器.我尝试了

MediaController mc= new MediaController();
mc.hide();
Videoview.setMediaController(mc);
..
..
..

但它不起作用..请问任何建议吗?

推荐答案

这不是隐藏MediaController的解决方案,但如果你想完全摆脱这件事,请执行以下操作:

videoView.setMediaController(null);

您可以通过执行上述功能来拥有它,然后当您希望它显示(onclick或Ontouch或其他)时,只需制作新的MediaController并将其设置为VideoView.我添加了一个布尔值,以防止操作不止一次.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        if (controllerCreated == false) {
            videoView.setMediaController(mc);
            mc.show();
            controllerCreated = true;
        }
        return true;
    } else {
        return false;
    }
}

其他推荐答案

指定videoView.setMediaController(null)不是必需的.

问题是您无法隐藏控制器,直至完全准备好.

使用onpreparedlistener,并在回调上onprepared of orderenty,如:

@Override
public void onPrepared (MediaPlayer mp) 
{
    int childs = mediaController.getChildCount();
    for (int i = 0; i < childs; i++)
    {
        View child = mediaController.getChildAt (i);
        child.setVisibility (View.GONE);
    }
}
不幸的是,这是VideoView中的硬编码行为:

...
if (mTargetState == STATE_PLAYING) {
    start();
    if (mMediaController != null) {
        mMediaController.show();
    }
...

作为解决方法,在您自己的类中包装MediaController并抑制初始show()呼叫,如下所示:

package pkg.your;

import android.content.Context;
import android.widget.MediaController;

public class MyMediaController extends MediaController {

    private boolean suppressed = true;

    public MyMediaController(Context context) {
        super(context);
    }

    @Override
    public void show(int timeout) {
        if (! suppressed) {
            super.show(timeout);
        }
        suppressed = false;
    }
}
然后,简单地将上面的MyMediaController常用连接,例如:

MyMediaController mc = mediaController = new MyMediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
....
现在,控件最初隐藏,并且当用户点击屏幕时,它们会显示到预期.

本文地址:https://www.itbaoku.cn/post/102400.html

问题描述

I have a videoview and when the video starts, the media controller is shown for 3 seconds. I want to hide the media controller unless i tap on the screen. I tried

MediaController mc= new MediaController();
mc.hide();
Videoview.setMediaController(mc);
..
..
..

But it didn't work.. Any suggestions please?

推荐答案

This isn't really a solution to hiding the MediaController, but if you want to get rid of the thing altogether, do this:

videoView.setMediaController(null);

You can have it initially hidden by doing the above, and then when you want it to show (onClick or onTouch or whatever), just make a new MediaController and set it on the videoView. I added a boolean to prevent the action from happening more than once.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        if (controllerCreated == false) {
            videoView.setMediaController(mc);
            mc.show();
            controllerCreated = true;
        }
        return true;
    } else {
        return false;
    }
}

其他推荐答案

Specifying videoView.setMediaController(null) is not necessary.

The problem is you cannot hide the controller till it fully prepared.

Use OnPreparedListener and in the callback onPrepared do your hide inderectly, like:

@Override
public void onPrepared (MediaPlayer mp) 
{
    int childs = mediaController.getChildCount();
    for (int i = 0; i < childs; i++)
    {
        View child = mediaController.getChildAt (i);
        child.setVisibility (View.GONE);
    }
}

其他推荐答案

Unfortunately, this is hardcoded behavior in VideoView:

...
if (mTargetState == STATE_PLAYING) {
    start();
    if (mMediaController != null) {
        mMediaController.show();
    }
...

As a workaround, wrap the MediaController in your own class and suppress the initial show() call, like this:

package pkg.your;

import android.content.Context;
import android.widget.MediaController;

public class MyMediaController extends MediaController {

    private boolean suppressed = true;

    public MyMediaController(Context context) {
        super(context);
    }

    @Override
    public void show(int timeout) {
        if (! suppressed) {
            super.show(timeout);
        }
        suppressed = false;
    }
}

Then, simply hookup the MyMediaController above as usual, e.g.:

MyMediaController mc = mediaController = new MyMediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
....

Now, the controls are initially hidden, and they show up as expected when the user taps the screen.