3

I'm using a MediaController to control video play back for my VideoView. I've overriden VideoView.setOnPreparedListener so that the ActionBar/Toolbar is hidden (hide()) after the video first finishes buffering. And I'd like the toolbar to comeback when the MediaController does. I've tried overriding the MediaController show() and hide() methods, like so:

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

This works, except the playback controls stop showing up! And of course, calling a recursive mMediaController.show() within the overridden method doesn't work... Can I have my cake an eat it too?

Edit

So I've also, unsucessfully tried taking advantage of the VideoView.setOnTouchListener and VideoView.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();
        }
    });

It does show, but it won't hide again, perhaps I misinterpret OnCompletetionListener?

Edit 2 From Amir's suggestion, I override the onTouch for VideoView, not perfect, but it's on like the right track:

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

This mostly works, it toggles the Toolbar and so sometimes the Toolbar will appear without the MediaController, and each time I toggle it, the MediaController does it's regular 'appear for a few seconds and then disappear.' In any case, it is a working solution.

Nevermore
  • 7,141
  • 5
  • 42
  • 64

2 Answers2

4

Your original code seems to work if you add calls to the corresponding super methods.

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();
    }
};
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Nukefuelled
  • 86
  • 1
  • 5
2

You should change your OnTouchListener a bit. I do following in my project and works fine:

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

And hideToolbar() with some traslate animation:

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

showToolbar():

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

Also if you need to HideStatusBar call setUiFlag() with true:

@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;
}
Amir
  • 16,067
  • 10
  • 80
  • 119
  • So I tried this, and it hasn't worked. I tried declaring the MediaController in several locations, including before the onCreate method (in order to declare `final`-- but when I do that, the app crashes because it cannot find the Context. Is that my issue? – Nevermore Aug 12 '16 at 18:53
  • @Nevermore it's not related to final value. which part of code cause exception? I just test this piece of code two hours ago and works. – Amir Aug 12 '16 at 18:54
  • @Nevermore also sometimes **setMediaController** cause exception put it in try catch. Also set Internet permission in manifest. – Amir Aug 12 '16 at 19:00
  • So no exception comes up, it simply does not show the toolbar. Also yes, the permissions are all there, in fact the app works great otherwise. I'll post exactly the code I've tried in another edit. – Nevermore Aug 12 '16 at 19:20
  • 1
    @Nevermore sorry instead of **MotionEvent.Action_UP** use **MontionEvent.Action_DOWN**. – Amir Aug 12 '16 at 19:55
  • So this works! Thanks! Except clicking will toggle the toolbar and the mediacontroller will hide and show like normal -- meaning that sometimes the toolbar will be there, until I toggle it off, that is, it doesn't just turn on and off with the mediacontroller. In any case, accepted! thanks a lot. – Nevermore Aug 12 '16 at 21:05
  • 1
    define custom SimpleTouchEventListener will fix this issue ;) – Amir Aug 13 '16 at 08:45