在 MediaController show() 和 hide() 上隐藏和显示工具栏

Hide and Show Toolbar on MediaController show() and hide()

我正在使用 MediaController 来控制 VideoView 的视频播放。我已经覆盖 VideoView.setOnPreparedListener 以便在视频首次完成缓冲后隐藏 ActionBar/Toolbar (hide())。当 MediaController 出现时,我希望工具栏 回归 。我试过 覆盖 MediaController show()hide() 方法,像这样:

    mVideoView.start();
    // Media Controller
    mMediaController = new MediaController(this){
        @Override
        public void show() {
            getSupportActionBar().show();
        }
        @Override
        public void hide() {
            getSupportActionBar().hide();
        }
    };
    mMediaController.setAnchorView(mVideoView);
    mVideoView.setMediaController(mMediaController);
    // Hide toolbar once video starts
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // Hide support bar
            getSupportActionBar().hide();
        }
    });

这有效,除了播放控件停止显示!当然,在重写的方法中调用递归 mMediaController.show() 是行不通的……我可以吃我的蛋糕吗?

编辑

所以我也尝试利用 VideoView.setOnTouchListenerVideoView.setOnCompletetionListener:

但没有成功
    mVideoView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            getSupportActionBar().show();
            return false;
        }
    });
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            getSupportActionBar().hide();
        }
    });

显示了,但不会再隐藏了,可能是我理解错了OnCompletetionListener

编辑 2 根据 Amir 的建议,我将 onTouch 覆盖为 VideoView,而不是 完美 ,但它在正确的轨道上:

    mMediaController = new MediaController(this);
    mVideoView = (VideoView) findViewById(R.id.media_player);
    mVideoView.setOnTouchListener(new View.OnTouchListener() {
        boolean flag;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    if(flag) {
                        mMediaController.hide();
                        getSupportActionBar().hide();
                    } else {
                        mMediaController.show();
                        getSupportActionBar().show();
                    }
                    flag = !flag;
                    return true;
            }
            return false;
        }
    });

这主要有效,它会切换 Toolbar,因此有时 Toolbar 会在没有 MediaController 的情况下出现,每次我切换它时,MediaController 都会出现它是常规的 'appear for a few seconds and then disappear.' 在任何情况下,它都是一个可行的解决方案。

你应该稍微改变一下你的 OnTouchListener。我在我的项目中做了以下工作并且工作正常:

final MediaController mediaController = new MediaController(this);
        VideoView videoView = (VideoView) findViewById(R.id.videoView);
        videoView.setOnTouchListener(new View.OnTouchListener() {
            boolean flag = true;
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (flag) {
                            mediaController.hide();
                            hideToolbar();
                        }
                        else {
                            mediaController.show(0);
                            showToolbar();
                        }
                        flag = !flag;
                        return true;

                }

                return false;
            }
        });

        videoView.setMediaController(mediaController);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {

            }
        });
        videoView.setVideoURI(Uri.parse("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"));

并且 hideToolbar() 带有一些翻译动画:

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

showToolbar():

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

此外,如果您需要 HideStatusBar 调用 setUiFlag() 为真:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setUiFlags(boolean fullscreen) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        View decorView = getWindow().getDecorView();
        if (decorView != null) {
            decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : View.SYSTEM_UI_FLAG_VISIBLE);
        }
    }
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getFullscreenUiFlags() {
    int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    return flags;
}

如果您添加对相应超级方法的调用,您的原始代码似乎可以工作。

mediaController = new MediaController(this){
    @Override
    public void show() {
        super.show();
        getSupportActionBar().show();
        Toast.makeText(activity_media_player.this, "SHOW TOOLBAR", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void hide() {
        super.hide();
        getSupportActionBar().hide();
        Toast.makeText(activity_media_player.this, "HIDE TOOLBAR", Toast.LENGTH_SHORT).show();
    }
};