android: hiding media controller functions

匆匆过客 提交于 2019-12-02 00:49:25

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.

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